在解析含有中文信息的XML数据时,总是时不时返回nil。
断点定位到 xmlParseMemory([data bytes], (int)[data length]); 这个方法上。
解析不对一般应该编码的问题。搜了一下资料,解决方法如下
- (instancetype)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error
{
if (data == nil || [data length] == 0)
{
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:0 userInfo:nil];
return nil;
}
// Even though xmlKeepBlanksDefault(0) is called in DDXMLNode's initialize method,
// it has been documented that this call seems to get reset on the iPhone:
// http://code.google.com/p/kissxml/issues/detail?id=8
//
// Therefore, we call it again here just to be safe.
xmlKeepBlanksDefault(0);
#// xmlDocPtr doc = xmlParseMemory([data bytes], (int)[data length]); 【有问题的代码】
# 两种修改方法:
#// xmlDocPtr doc = xmlRecoverMemory([data bytes], [data length]);【方案一】
# xmlDocPtr doc = xmlReadMemory([data bytes],(int)[data length],NULL,"gbk",XML_PARSE_RECOVER);【方案二】
if (doc == NULL)
{
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:nil];
return nil;
}
return [self initWithDocPrimitive:doc owner:nil];
}
网友评论