XML解析文本编码问题

发布于 2024-11-09 15:13:38 字数 956 浏览 0 评论 0原文

我需要解析带有法语口音(例如 'é' )的 XML 提要。 当我解析 XML 时,我丢失了重音符号...

我的代码出了什么问题...

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];

当我开始解析时...

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

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

    self.currentString = [NSMutableString string];
    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 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];
    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

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

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

发布评论

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

评论(2

吲‖鸣 2024-11-16 15:13:38

这似乎是 stringWithContentsOfURL 的编码问题

尝试:

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

It appears to be an encoding issue with stringWithContentsOfURL

Try:

NSError *lookupError = nil;
NSString *myData = [NSString stringWithContentsOfURL:myUrl
encoding:NSUTF8StringEncoding error:&lookupError];
时常饿 2024-11-16 15:13:38

您首先读取 XML 数据并将其转换为 NSString,然后将该字符串转换回数据。正如 krtrego 所说,字符串转换可能选择了错误的编码。相反,您应该将 URL 中的原始数据直接提供给 NSXMLParser,这样可以更智能地解释正确的编码(即使用 XML 文件顶部的元数据)。

因此,请

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

在初始化时使用 then pass 该数据 。 NSXML解析器。

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.

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