编码换行符NSXMLElement属性值中的字符

编码换行符NSXMLElement属性值中的字符

本文介绍了编码换行符NSXMLElement属性值中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在读取和写入的XML数据库的注释字段存储为NSXMLElement的属性。一个条目包含换行符(0x0a)字符。这些是由非NSXML编码器编码的,我在解析为& #xA; ,并得到NSXML正确解析。它们导致NSString在内存中包含unicode值0x0a 0x00(intel字节排序)。



例如:

 < INFO BITRATE =192000GENRE =PodcastCOMMENT =测试& amp;更多测试& #xA;& #xA; ; / INFO> 

当将NSString写回NSXMLElement属性的值时,导致以下输出到xml文件:

 < INFO BITRATE =192000GENRE =PodcastCOMMENT = 测试& amp;更多测试

换行符< / INFO>

当重新读取文件时,当然无法正确解析。



它似乎像节点选项像 NSXMLNodePreserveCharacterReferences NSXMLNodePreserveEntities 应该是



我必须缺少明显的,但我一直被困在这一整天。

解决方案

使用Apple检查,这是NSXMLDocument的一个已知问题,现在已作为错误报告提交。



建议的解决方法包括暂时使用libxml2。



如果我在未来的版本中看到这个问题,我会发表评论。 >

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 &#xA; 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.

解决方案

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.

这篇关于编码换行符NSXMLElement属性值中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:18