开发者

XML parsing text encoding problem

I need to parse a XML feed with french accent like 'é' . When I parse the XML I lost the accents...

What's wrong in my code...

NSString *url = [NSString stringWithFormat:@"%@",@"my_xml_url"];
NSURL *myURL = [NSURL URLWithString:url];
NSString *myData = [[NSString alloc] initWithContentsOfURL:myURL];
XMLParser *parser = [[XMLParser alloc] init];
[parser parseXMLFile:myData];

And when I start parsing.....

- (void)parseXMLFile:(NSString *)data {
    BOOL success;

    //array for the ranking
    rows = [[NSMutableArray alloc] init];
    index = 0;

    self.currentString = [NSMutableString string];
    开发者_Python百科storingCharacters = NO;
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[data data UsingEncoding:NSUTF8StringEncoding]];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:YES];
    success = [parser parse];
    self.currentString = nil;

    //release memory
    [parser release];
}

I can't see where is the problem...

Thanks


It appears to be an encoding issue with stringWithContentsOfURL

Try:

NSError *lookupError = nil;
NSString *myData = [NSString stringWithContentsOfURL:myUrl
encoding:NSUTF8StringEncoding error:&lookupError];


You're first reading the XML data and converting it to an NSString, then later converting that string back into data. As krtrego said, the to-string conversion is probably picking the wrong encoding. Instead, you should feed the raw data from the URL directly into NSXMLParser, which is smarter about being able to interpret the correct encoding (i.e. using the metadata at the top of the XML file.)

So instead use

NSData* myData = [[NSData dataWithContentsOfURL: url options: 0 error: &error];

then pass that data when initializing the NSXMLParser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜