Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why XML tag data inside this <![CDATA[]]>

Tags:

xml

I have seen in many XML file has the data inside this <![CDATA[ ]]>. What is the use of this?

like image 723
Mohamed Saligh Avatar asked Jun 25 '26 19:06

Mohamed Saligh


2 Answers

From Wikipedia

A CDATA section starts with the following sequence:

<![CDATA[

and ends with the first occurrence of the sequence:

]]>

All characters enclosed between these two sequences are interpreted as characters, not markup or entity references. For example, in a line like this:

<sender>John Smith</sender>

the opening and closing "sender" tags are interpreted as markup. However, if written like this:

<![CDATA[<sender>John Smith</sender>]]>

then the code is interpreted the same as if it had been written like this:

&lt;sender&gt;John Smith&lt;/sender&gt;

Check out the Wiki page for more details.

like image 120
Neil Knight Avatar answered Jun 27 '26 16:06

Neil Knight


See CDATA at Wikipedia:

In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup.

That means the parser will not interpret any data contained in <![CDATA[ ]]>.

Lets say you want to store HTML as text in an XML document. This is wrong:

<root>
    <myhtml>
        <div><b>Foo bar</b></div>
    </myhtml>
</root>

The parser would try to parse <div><b>Foo bar</b></div> and create DOM elements. It would work in this example, but not for documents that conform to an DTD.

Instead, we want this:

<root>
    <myhtml>
        <![CDATA[ <div><b>Foo bar</b></div> ]]>
    </myhtml>
</root>

and <div><b>Foo bar</b></div> will become the content of a text node.

like image 40
Felix Kling Avatar answered Jun 27 '26 16:06

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!