我们如何解析采用 ISO-8859-15 编码的 XML?

发布于 2024-09-26 22:49:29 字数 227 浏览 2 评论 0原文

我使用 UTF-8 编码的 xml 来使用 NSXMLParser 进行解析。但一些特殊字符引起了问题,因此决定使用 ISO-8859-15 编码。

但之后解析器甚至没有开始解析并给出错误 31 - NSXMLParserUnknownEncodingError。我现在该怎么办?无论如何,我们是否可以在 iphone 中解析 ISO-8859-15 编码的 xml? libxml 或任何其他解析器会提供对此编码的支持吗?

I was using UTF-8 encoded xml for parsing using NSXMLParser. But some of the special characters were causing problems and so decided to use ISO-8859-15 encoding.

But after that the parser doesnt even start parsing and is giving the error 31 - NSXMLParserUnknownEncodingError. What should I do now? Is it possible by anyway we can parse a ISO-8859-15 encoded xml in iphone? Will libxml or anyother parser provide support for this encoding?

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

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

发布评论

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

评论(2

梦言归人 2024-10-03 22:49:29

我自己解决了这个问题。我们决定对 xml 使用 UTF-8 编码,并使用 NSXMLParser 对其进行解析。但在解析之前我们做了以下步骤。

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://the url for parsing"]];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSString *result = [[[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding] autorelease];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[result dataUsingEncoding:NSUTF8StringEncoding]];

之后,一些意大利字符(3-4)仍然引起问题,我们在得到结果后手动替换它们。

现在一切正常。

I solved this issue by myself. We decided to use the UTF-8 encoding for the xml and parsed it using the NSXMLParser. But before the parsing we did the following steps.

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://the url for parsing"]];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSString *result = [[[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding] autorelease];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[result dataUsingEncoding:NSUTF8StringEncoding]];

Then after that some italian characters(3-4) were still causing problems and we manually replaced them after getting the result.

Now everything works fine.

滥情哥ㄟ 2024-10-03 22:49:29

NISOLatin1StringEncoding 是错误的,例如 € 无法编码。

xml 文件:

<?xml version="1.0" encoding="ISO-8859-15"?>
...

xCode:

NSString *xmlFileAsString = nil; //read xml file

NSStringEncoding iso88599 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin9); 

NSData *data = [xmlFileAsString dataUsingEncoding:iso88599 allowLossyConversion:YES];

xmlDocPtr doc = xmlReadMemory([data bytes], (int)[data length], NULL, NULL, XML_PARSE_COMPACT | XML_PARSE_NOBLANKS);

if (doc == NULL) {
...

从 xml 获取字符串:(例如 USAdditions.m)

+ (NSString *)stringWithXmlString:(xmlChar *)str free:(BOOL)freeOriginal {
    if (!str) return nil;
    NSString *string = [NSString stringWithCString:(char*)str encoding:NSUTF8StringEncoding];
    if (freeOriginal)
        xmlFree(str);
    return string;
}

NSISOLatin1StringEncoding is wrong, e.g. € can't encode.

xml file:

<?xml version="1.0" encoding="ISO-8859-15"?>
...

xCode:

NSString *xmlFileAsString = nil; //read xml file

NSStringEncoding iso88599 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin9); 

NSData *data = [xmlFileAsString dataUsingEncoding:iso88599 allowLossyConversion:YES];

xmlDocPtr doc = xmlReadMemory([data bytes], (int)[data length], NULL, NULL, XML_PARSE_COMPACT | XML_PARSE_NOBLANKS);

if (doc == NULL) {
...

get string from xml: (e.g. USAdditions.m)

+ (NSString *)stringWithXmlString:(xmlChar *)str free:(BOOL)freeOriginal {
    if (!str) return nil;
    NSString *string = [NSString stringWithCString:(char*)str encoding:NSUTF8StringEncoding];
    if (freeOriginal)
        xmlFree(str);
    return string;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文