开发者

Trying to parse non well-formed XML using NSXMLParser

I am pa开发者_开发问答rsing 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!


To answer half your question, XML has 5 special characters that you may want to escape:

< -- replace with &lt;

> -- replace with &gt;

& -- replace with &amp;

' -- replace with &apos;

and

" -- replace with &quot;

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 &quot;quotes&quot;" />


You should encode these characters for instance & becomes &amp; or " becomes &quot;

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜