Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What does <![CDATA[]]> in XML mean?

I regularly track down this strange CDATA tag in XML records:
<![CDATA[some stuff]]>

I have seen that this CDATA tag consistently comes toward the start, and afterwards followed by some stuff.

Yet, once in a while it is utilized, here and there it isn't. I expect it is to check that some stuff is the "data" that will be embedded after that. Yet, what sort of data is some stuff? Isn't anything I write in XML tags some sort of data?
by

2 Answers

ninja01
I once had to use CDATA when my xml element needed to store HTML code. Something like

<codearea>
<![CDATA[
<div> <p> my para </p> </div>
]]>
</codearea>

So CDATA means it will ignore any character which could otherwise be interpreted as XML tag like < and > etc.
MounikaDasa
One big use-case: your xml includes a program, as data (e.g. a web-page tutorial for Java). In that situation your data includes a big chunk of characters that include '&' and '<' but those characters aren't meant to be xml.

Compare:

<example-code>
while (x &lt; len &amp;&amp; !done) {
print( &quot;Still working, &apos;zzz&apos;.&quot; );
++x;
}
</example-code>
with

<example-code><![CDATA[
while (x < len && !done) {
print( "Still working, 'zzzz'." );
++x;
}
]]></example-code>
Especially if you are copy/pasting this code from a file (or including it, in a pre-processor), it's nice to just have the characters you want in your xml file, w/o confusing them with XML tags/attributes. As @paary mentioned, other common uses include when you're embedding URLs that contain ampersands. Finally, even if the data only contains a few special characters but the data is very very long (the text of a chapter, say), it's nice to not have to be en/de-coding those few entities as you edit your xml file.

Login / Signup to Answer the Question.