CDATA Section within a CDATA (or “How to escape ]]> in CDATA?”)
Tuesday, February 20th, 2007Using CDATA to embed raw data within XML is a quite convinient feature. But the XML spec lacks the possibility to have a CDATA containing another CDATA or even the string ]]>.
So what happens if you put content like this within a CDATA section?
<![CDATA[
Just a ]]> test
]]>
Just a ]]> test
]]>
Your XML writer or at least any descent parser will complain it is not valid XML! Even the color coding on this page reveals that
What to do?
The easiest (and IMHO most compatible) way of handling this is to build multiple CDATA sections, like this:
<![CDATA[
Just a ]]>]]><![CDATA[test
]]>
Just a ]]>]]><![CDATA[test
]]>