Take the 2-minute tour ×

I have an XML data source that has HTML & CSS formatted data contained in one of the document nodes. What is the proper way to escape this data so that I can properly parse it? For clarification, I am using TouchXML in Objective-C to parse the data. (Not that it should matter but I wanted to include all pertinent information.)

Any help would be much appreciated. Thank you!


share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I'm assuming you have XML data that will have nodes containing HTML markup, not a mixture of XHTML and XML data intertwined in your document.

In this case, I generally prefer to use CDATA blocks for HTML data. This allows you to put any HTML you want. Trying to add HTML as regular nodes in an XML document can cause problems due to the fact that it is not always 100% XML compliant, as well as the fact that character entities would need to be defined in order to be properly recognized by an XML parser.


                <Want />

share|improve this answer
Note that it's possible for the string "]]>" to appear in HTML and CSS, so you need to be careful that your CDATA block isn't inadvertently terminated. You could do this by replacing "]]>" with "]]]]><![CDATA[>" (ie: end the CDATA block part way through the sequence, and start a new one). An alternate approach is just to use &-escaping instead of CDATA blocks. Whether you use CDATA blocks or &-escaping, they'll look the same to your XML parser. –  Laurence Gonsalves Jan 26 '10 at 5:26
@Laurence, while your point is valid, it is not very likely for the string "]]>" to appear, since CDATA is not common (to my knowledge) in HTML and it would be rare to include that sequence unescaped to display as regular output in a browser. It should be taken into consideration though, obviously. –  Dan Herbert Jan 26 '10 at 14:20
Let's just say that I've seen a lot of code that takes a hand-wavy approach to escaping only to be bitten by it later when the "unlikely" ends up actually happening. –  Laurence Gonsalves Jan 26 '10 at 23:04
You both have given me the information I need. I'll try encapsulating my HTML in "<![CDATA" blocks and see if that does the trick. Thank you both again. –  Leachy Peachy Jan 27 '10 at 0:46
add comment

Your Answer


