Encoding Line Feed Characters in the value of a NSXMLElement's attribute

十年热恋 提交于 2020-01-02 23:14:39

问题


The comment field of an XML database I'm reading and writing is stored as the attribute of an NSXMLElement. One entry contains a line feed (0x0a) character. These are encoded by a non NSXML encoder in the document I'm parsing as 
 and get parsed correctly by NSXML. They result in the NSString containing the unicode value 0x0a 0x00 in memory (intel byte ordering).

For example:

<INFO BITRATE="192000" GENRE="Podcast" COMMENT="Test &amp; More Test &#xA;&#xA;After the Line Feeds"</INFO>

When writing this NSString back out as the value of an NSXMLElement's attribute, it does not get encoded back and results in the following being output to the xml file:

 <INFO BITRATE="192000" GENRE="Podcast" COMMENT="Test &amp; More Test 

After the Line Feeds"</INFO>

which of course does not get parsed properly upon re-reading the file again.

It seems like node options like NSXMLNodePreserveCharacterReferences or NSXMLNodePreserveEntities should be the way to go but it doesn't seem to help in any way.

I must be missing the obvious but I've been stuck on this all day.


回答1:


Checked with Apple, this is a known issue with NSXMLDocument and it has now been filed as a bug report.

Suggested workaround include using libxml2 for the time being.

I will post a comment if I see this fixed in a future release.




回答2:


Apple says:

The options with “Preserve” in their names are applicable only when external sources of XML are parsed; they have no effect on node objects that are programmatically created. Other options are used in initialization and output methods of NSXMLDocument; see the NSXMLDocument reference documentation for details.` It dose not work for me, I think it is a bug.

I solved this problem with KissXML.



来源:https://stackoverflow.com/questions/10134649/encoding-line-feed-characters-in-the-value-of-a-nsxmlelements-attribute

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