NSXMLParser 替换 é带有 \U00e9 的字符

发布于 2024-12-05 06:50:17 字数 2786 浏览 0 评论 0原文

我正在使用XML解析器,nsxmlparsernsmutablearray中解析a xml asn xml返回某些url法语\ u00e9取代。

这是我的代码:

- (void)parseXMLFileAtURL:(NSString *)URL
{
    NSURL *xmlURL = [NSURL URLWithString:URL];

    xmlParser = [NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [xmlParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];

    [xmlParser parse];
}



- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{       

    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"catalogue"]) {
        // clear out our story item caches...
        currentCatalogue = [[Catalogue alloc] init];
    }

    if ([elementName isEqualToString:@"partenaire"]) {
        // clear out our story item caches...
        currentPartenaire = [[Partenaire alloc] init];
    }
}

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"catalogue"]) {       
            // Add currentCatalogue to array
            [catalogueList addObject: currentCatalogue];
            NSString *urls=[catalogueList valueForKey:@"url"];
        NSLog(@"Current catalogue: urls=%@", urls);
        }

        if ([elementName isEqualToString:@"partenaire"]) {      
            // Add currentPartenaire to array
            [partenaireList addObject: currentPartenaire];
            /*NSLog(@"Current partenaire: raison_sociale=%@, lat=%@, lng=%@", currentPartenaire.raison_sociale, currentPartenaire.lat, currentPartenaire.lng);*/
        }
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        // Catalogue setup
        if ([currentElement isEqualToString:@"id_model"])
            currentCatalogue.id_model = string;

        if ([currentElement isEqualToString:@"url"])
        {
            if (currentCatalogue.url)
            {
                currentCatalogue.url = [NSString stringWithFormat: @"%@%@", currentCatalogue.url, string];

             //   NSLog(@"Valoare url in data handler %@", currentCatalogue.url);
            }
            else
                currentCatalogue.url = string;


        }
    }

有人知道如何解决这个问题吗?

I'm using an xml parser, NSXMLParser to parse asn xml an return some url in an NSMutableArray.Everything is giong great except the fact that the french é is replaced by \U00e9.

Here is my code:

- (void)parseXMLFileAtURL:(NSString *)URL
{
    NSURL *xmlURL = [NSURL URLWithString:URL];

    xmlParser = [NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [xmlParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];

    [xmlParser parse];
}



- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{       

    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"catalogue"]) {
        // clear out our story item caches...
        currentCatalogue = [[Catalogue alloc] init];
    }

    if ([elementName isEqualToString:@"partenaire"]) {
        // clear out our story item caches...
        currentPartenaire = [[Partenaire alloc] init];
    }
}

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"catalogue"]) {       
            // Add currentCatalogue to array
            [catalogueList addObject: currentCatalogue];
            NSString *urls=[catalogueList valueForKey:@"url"];
        NSLog(@"Current catalogue: urls=%@", urls);
        }

        if ([elementName isEqualToString:@"partenaire"]) {      
            // Add currentPartenaire to array
            [partenaireList addObject: currentPartenaire];
            /*NSLog(@"Current partenaire: raison_sociale=%@, lat=%@, lng=%@", currentPartenaire.raison_sociale, currentPartenaire.lat, currentPartenaire.lng);*/
        }
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        // Catalogue setup
        if ([currentElement isEqualToString:@"id_model"])
            currentCatalogue.id_model = string;

        if ([currentElement isEqualToString:@"url"])
        {
            if (currentCatalogue.url)
            {
                currentCatalogue.url = [NSString stringWithFormat: @"%@%@", currentCatalogue.url, string];

             //   NSLog(@"Valoare url in data handler %@", currentCatalogue.url);
            }
            else
                currentCatalogue.url = string;


        }
    }

Anyone any idea how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-12-12 06:50:17

测试的代码:100%工作

NSString* inputString =[NSString stringWithFormat:@"\40_TTRS_Coup\u00e9_TTRS_Roadster_Tarifs_20110428.pdf"];


NSLog(@"inputString is: %@ \n\n",inputString);

输出:

  inputString is:  _TTRS_Coupé_TTRS_Roadster_Tarifs_20110428.pdf 

TESTED CODE : 100 % WORKS

NSString* inputString =[NSString stringWithFormat:@"\40_TTRS_Coup\u00e9_TTRS_Roadster_Tarifs_20110428.pdf"];


NSLog(@"inputString is: %@ \n\n",inputString);

OUTPUT:

  inputString is:  _TTRS_Coupé_TTRS_Roadster_Tarifs_20110428.pdf 
风透绣罗衣 2024-12-12 06:50:17

假设您的XML具有明确的编码,则如下:

 <?xml version="1.0" encoding="UTF-8"?>

然后您应该首先解决两个问题。这些可能会或可能不会解决您的直接问题,但是如果没有,它们将使您在看到的问题上更容易缩小。这是您应该解决的问题:

  1. 删除单个N​​SXMLPARSER实例上调用2个不同初始化方法的代码。这将带来不确定的结果,直到您修复它之前,就不可能知道发生了什么。
  2. 更改您实现解析器的方式:fundCharacter:方法。正如NSXMLParser文档所述,对于XML元素中的一组字符,这可以多次称为。您的代表类不仅接受字符串并将其值存储为单位,还应具有可变的字符缓冲区,您将每次调用undercharacters。然后在解析器中:didelementend您可以抓住缓冲区的内容,并以该值进行需要。

尝试使用这些修复程序,看看它是否有效。如果没有,请使用代码的更正版本更新您的帖子,这可能是什么问题。

Assuming your XML has an explicit encoding, as follows:

 <?xml version="1.0" encoding="UTF-8"?>

then there are 2 problems you should address first. These may or may not fix your direct problem, but if not they will make it easier to narrow in on the problem you are seeing. Here are the problems you should fix:

  1. Remove the code that calls 2 different initializer methods on a single NSXMLParser instance. That will have undefined results and its impossible to know what is going on until you fix that.
  2. Change how you implement the parser:foundCharacters: method. As NSXMLParser documentations states, this may be called multiple times for a given set of characters within an XML element. Instead of just accepting the string and storing its value away, your delegate class should have a mutable character buffer that you append to each time foundCharacters gets called. Then in parser:didElementEnd you can grab the contents of the buffer and do what you need to with that value.

Try it out with these fixes and see if it works. If not, update your post with a corrected version of your code and it might be more obvious what the problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文