尝试使用 NSXMLParser 解析格式不正确的 XML
我正在使用 NSXMLParser 解析 XML 数据,现在我注意到元素可以包含所有字符,例如包括 &
。由于解析器在遇到此字符时会给出错误,因此我替换了此字符的每个出现位置。 现在我想确保处理所有可能导致错误的字符。 它们是什么?你认为我应该如何最好地处理这些角色? 提前致谢!
I am parsing XML Data using NSXMLParser and I notice now, that the Elements can contain ALL characters, including for example a &
. Since the parser is giving an error when it comes across this character I replaced every Occurence of this character.
Now I want to make sure to handle every of these characters that may cause Errors.
What are they and how do you think I should handle these characters best?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了回答您一半的问题,XML 有 5 个您可能需要转义的特殊字符:
<< -- 替换为 <
> -- 替换为 >
& -- 替换为 &
' -- 替换为 '
和
" -- 替换为 "
现在,对于另一半 -- 如何查找和替换它们而不替换所有标签等......并不容易,但我会研究正则表达式和 NSRegularExpression: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
请记住,根据您的用例,对值进行转义标签上的参数也是如此;
To answer half your question, XML has 5 special characters that you may want to escape:
< -- replace with <
> -- replace with >
& -- replace with &
' -- replace with '
and
" -- replace with "
Now, for the other half--how to find and replace these without also replacing all the tags, etc... Not easy, but I'd look in to regular expressions and NSRegularExpression: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
Remember, depending on your use case, to escape the values of the parameters on tags, too; <tag parameter="with "quotes"" />
您应该对这些字符进行编码,例如 &变为
&
或 " 变为"
当它通过解析器时,结果应该正常。您的另一个选择是使用不同的 XML 解析器,例如 TBXML它不进行格式检查。
You should encode these characters for instance & becomes
&
or " becomes"
When it goes through the parser it should come out ok. Your other option is to use a different XML parser like TBXML which doesn't do format checking.