问题
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 & More Test 

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 & 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