NSString to NSData Failing in Encoding

六眼飞鱼酱① 提交于 2019-12-13 03:48:53

问题


I'm trying to use NSXmlParser to parse ISO-8859-1 data. Using Apple's own example for parsing ISO-8859-1, I have the following.

// path to xml file
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:sampleFileName ofType:@"xml"];

// string of xml contents
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"contents: %@", xmlFileContents);

I see that in the console, the contents of the string is accurate.

However when I try to convert it to an NSData object (for use with the parser), I do the following.

NSData *xmlData = [xmlFileContents dataUsingEncoding:NSUTF8StringEncoding];

But then when my didStartElement delegate gets called, I see  showing up which I think is from an encoding discrepancy.

Can NSXmlParser handle ISO-8859-1 and if so, what am I doing wrong?


回答1:


Just in case anyone else ends up on this thread trying to figure out how the heck to get XML that starts with <?xml version="1.0" encoding="ISO-8859-1"?> read properly by NSXmlParser, here is what I got working.

// path to xml file
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:sampleFileName ofType:@"xml"];

// string of xml contents (read in NSUTF8StringEncoding)
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath encoding:NSUTF8StringEncoding error:nil];

// interpret string of XML contents as ISO-8859-1 (NSISOLatin1StringEncoding)
NSData *xmlData = [xmlFileContents dataUsingEncoding:NSISOLatin1StringEncoding];

// spawn new thread to parse data
[NSThread detachNewThreadSelector:@selector(parseLineData:) toTarget:self withObject:xmlData];

Reading in the XML contents as NSUTF8StringEncoding and then into NSData as NSUTF8StringEncoding was the only way I avoided the spurious  characters.



来源:https://stackoverflow.com/questions/2485920/nsstring-to-nsdata-failing-in-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!