Search code examples
xmlcocoansxmlparsernsxmldocumentnsxmlelement

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


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.


Solution

  • 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.