xcode NSXMLParser 无法解析特殊字符(重音)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10215221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:00:47  来源:igfitidea点击:

NSXMLParser can't parse special characters (accents)

xcodexml-parsingnsxmlparsernsxmlnsxmlelement

提问by Andrew Davis

I'm using NSXMLParser to parse an xml from a url (my code is almost exactly the same as here)

我正在使用 NSXMLParser 从 url 解析 xml(我的代码几乎与此处完全相同)

Some of the elements contain special characters like "á" which causes a word lik ándre to split into two (á and ndre).

某些元素包含特殊字符,例如“á”,这会导致单词 lik ándre 分裂为两个(á 和 ndre)。

Here is my loadXMLByURL

这是我的 loadXMLByURL

-(id) loadXMLByURL:(NSString *)urlString{
tickets     = [[NSMutableArray alloc] init];
NSURL *url      = [NSURL URLWithString:urlString];
NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
parser          = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;}

I'm pretty sure it's because the encoding is not set (I think it needs to be NSUTF8StringEncoding) but I'm not sure where/how to apply it.

我很确定这是因为未设置编码(我认为它需要是 NSUTF8StringEncoding)但我不确定在哪里/如何应用它。

[UPDATE] Rest of my code...

[更新] 我的其余代码...

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{    
    if ([elementname isEqualToString:@"ticket"]) 
    {
        currentTicket = [Ticket alloc];
    }

}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementname isEqualToString:@"name"]) 
    {
        currentTicket.name = currentNodeContent;
    }
    else if ([elementname isEqualToString:@"title"]) 
    {
        currentTicket.title = currentNodeContent;
    }
    else if ([elementname isEqualToString:@"status"]) 
    {
        currentTicket.status = currentNodeContent;
    }
    else if ([elementname isEqualToString:@"ticket"])
    {
        [tickets addObject:currentTicket];
        [currentTicket release];
        currentTicket = nil;
        [currentNodeContent release];
        currentNodeContent = nil;
    }

}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

[UPDATE 2] Sample xml...

[更新 2] 示例 xml...

<RB>
    <list>
        <ticket>
            <name>Andrew Ford</name>
            <title>3rd release</title>
            <status>1</status>
        </ticket>

        <ticket>
            <name>David Jenkins</name>
            <title>3rd release</title>
            <status>0</status>
        </ticket>

        <ticket>
            <name>Luis gomez ándre</name>
            <title>3rd release</title>
            <status>1</status>
        </ticket>
    </list>
</RB>

采纳答案by Jeff Wolski

I would load the url to an NSStringand then convert like this.

我会将 url 加载到 anNSString然后像这样转换。

-(id) loadXMLByURL:(NSString *)urlString{

    tickets     = [[NSMutableArray alloc] init];
    NSURL *url      = [NSURL URLWithString:urlString];
    NSError *error;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    parser          = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;

}

EDIT: Part of the problem may be that your parser:foundCharacters:method is assigning to your currentNodeContentinstead of appending. See the Apple Doc at the following link.

编辑:部分问题可能是您的parser:foundCharacters:方法正在分配给您currentNodeContent而不是附加。请参阅以下链接中的 Apple 文档。

http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html

From the doc:

从文档:

Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

因为字符串可能只是当前元素总字符内容的一部分,所以您应该将其附加到当前的字符累积中,直到元素发生变化。

回答by Hyman Solomon

Found the problem! It is indeed in found characters. You should change your code to this:

发现问题了!它确实是在找到的字符中。您应该将代码更改为:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"found characters: %@", string);
    if (!currentNodeContent) {
        currentNodeContent = [[NSMutableString alloc] init];
    }
    [currentNodeContent appendString:string];
}

I was having the same problem before, and the above code has fixed it.

我之前也遇到过同样的问题,上面的代码已经解决了。

回答by Raymond Wang

Use

NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

and to get the string from it, do this:

并从中获取字符串,请执行以下操作:

NSString *theXML = [[NSString alloc] initWithBytes:[data mutableBytes]
                                                    length:[data length]
                                                 encoding:NSUTF8StringEncoding];

Then you can parse the xml in your NSXMLParserDelegate methods.

然后你可以在你的 NSXMLParserDelegate 方法中解析 xml。

Hope this helps.

希望这可以帮助。