I often find this strange CDATA
tag in XML
files:
I have observed that this CD
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.
It's used to contain data which could otherwise be seen as xml because it contains certain characters.
This way the data inside will be displayed, but not interpreted.
CDATA stands for Character Data. You can use this to escape some characters which otherwise will be treated as regular XML. The data inside this will not be parsed.
For example, if you want to pass a URL that contains &
in it, you can use CDATA to do it. Otherwise, you will get an error as it will be parsed as regular XML.
CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be.
The key differences between CDATA and comments are:
]]>
(CDEnd), while in a comment -- is invalid.This means given these four snippets of XML from one well-formed document:
<!ENTITY MyParamEntity "Has been expanded">
<!--
Within this comment I can use ]]>
and other reserved characters like <
&, ', and ", but %MyParamEntity; will not be expanded
(if I retrieve the text of this node it will contain
%MyParamEntity; and not "Has been expanded")
and I can't place two dashes next to each other.
-->
<![CDATA[
Within this Character Data block I can
use double dashes as much as I want (along with <, &, ', and ")
*and* %MyParamEntity; will be expanded to the text
"Has been expanded" ... however, I can't use
the CEND sequence. If I need to use CEND I must escape one of the
brackets or the greater-than sign using concatenated CDATA sections.
]]>
<description>An example of escaped CENDs</description>
<!-- This text contains a CEND ]]> -->
<!-- In this first case we put the ]] at the end of the first CDATA block
and the > in the second CDATA block -->
<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>
<!-- In this second case we put a ] at the end of the first CDATA block
and the ]> in the second CDATA block -->
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
The data contained therein will not be parsed as XML, and as such does not need to be valid XML or can contain elements that may appear to be XML but are not.
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 < len && !done) {
print( "Still working, 'zzz'." );
++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.
(I suspect all the comparisons to comments are kinda misleading/unhelpful.)