tags:

views:

220

answers:

5

I often find this strange CDATA tag in XML files:

<![CDATA[]]>

I have observed that this CDATA tag always comes at the beginning, and then followed by some stuff.

But sometimes it is used, sometimes it is not. I assume it is to mark that some "data" will be inserted after that. But what kind of "data"? Isn't anything I write in XML tags some sort of "data"?

+4  A: 

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.

fbrereto
+9  A: 

CDATA stands for Character Data and it means that the data in between these tags includes data that could be interpreted as XML markup, but should not be.

See: http://www.w3.org/TR/REC-xml/#sec-cdata-sect

Sean Vieira
A: 

It's used to contain data which could me otherwise be seen as xml because it contains certain characters.

This way the data inside will not be interpreted.

Ikke
A: 

Usually used for embedding custom data, like pictures or sound data within an XML document.

Johan
Although you could put text-encoded binary data in a CDATA section, you don't have to, because CDATA has nothing directly to do with binary anything.
Joel Mueller
+3  A: 

A CDATA section is "a section of element content that is marked for the parser to interpret as only character data, not markup."

Syntactically, it behaves like a comment:

<exampleOfAComment>
<!--
    Since this is a comment
    I can use all sorts of reserves characters
    like > < " and &
    or write things like
    <foo></bar>
    but my document is still well-formed!
-->
</exampleOfAComment>

... but it is still part of the document:

<exampleOfACDATA>
<![CDATA[
    Since this is a CDATA section
    I can use all sorts of reserves characters
    like > < " and &
    or write things like
    <foo></bar>
    but my document is still well formed!
]]>
</exampleOfACDATA>

Try saving the following as a .xhtml file (not .html) and open it using FireFox (not Internet Explorer) to see the difference between the comment and the CDATA section; the comment won't appear when you look at the document in a browser, while the CDATA section will:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>CDATA Example</title>
</head>
<body>

<h2>Using a Comment</h2>
<div id="commentExample">
<!--
You won't see this in the document
and can use reserved characters like
< > & "
-->
</div>

<h2>Using a CDATA Section</h2>
<div id="cdataExample">
<![CDATA[
You will see this in the document
and can use reserved characters like
< > & "
]]>
</div>

</body>
</html>
LeguRi