XML解析文本编码问题
我需要解析带有法语口音(例如 'é' )的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是 stringWithContentsOfURL 的编码问题
尝试:
It appears to be an encoding issue with stringWithContentsOfURL
Try:
您首先读取 XML 数据并将其转换为 NSString,然后将该字符串转换回数据。正如 krtrego 所说,字符串转换可能选择了错误的编码。相反,您应该将 URL 中的原始数据直接提供给 NSXMLParser,这样可以更智能地解释正确的编码(即使用 XML 文件顶部的元数据)。
因此,请
在初始化时使用 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
then pass that data when initializing the NSXMLParser.