【发布时间】:2012-02-01 19:20:48
【问题描述】:
我正在使用 libXML2 读取从后端系统检索到的 XML 的 iOS 应用程序。我有以下 XML,它是较大 XML 文档的一部分:
<properties uiValue="This is a multiline description with text that should wrap but should also preserve any whitespace: like this whitespace.
And preserve newlines.
espace:~` !@#$%^&*()_+=-<>/ \" name="desc">
<values value="This is a multiline description with text that should wrap but should also preserve any whitespace: like this whitespace.
And preserve newlines.
espace:~` !@#$%^&*()_+=-<>/ \"/>
</properties>
作为一个整体,文档似乎解析OK。我遇到的问题是换行符没有被处理,所以当我读取属性值时,结果是:
This is a multiline description with text that should wrap but should also preserve any whitespace: like this whitespace. And preserve newlines. espace:~` !@#$%^&*()_+=-<>/
有什么办法可以保留这些新行吗?如果我直接从服务器打印出响应 XML,则会保留新行。但是,当我进行解析时,会删除新行。更复杂的是,这是我正在尝试修复的一些第三方代码,我并没有真正使用过 libXML2。相关代码(我相信)是:
NSLog(@"Response:\n%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
xmlDocPtr doc = xmlReadMemory([data bytes], [data length], NULL, NULL, XML_PARSE_COMPACT | XML_PARSE_NOBLANKS);
xmlNodePtr cur = ....;
xmlChar *attrValue = xmlGetProp(cur, (const xmlChar *) "uiValue");
NSString *attrString = [NSString stringWithCString:(char*)attrValue encoding:NSUTF8StringEncoding];
我尝试取消 XML_PARSE_COMPACT 和 XML_PARSE_NOBLANKS 选项,但这没有帮助(不是我期望的,我相信这些只会影响节点)。
【问题讨论】:
标签: objective-c ios xml libxml2