CDATA is a special comment tag within an XML document. It basically says, "everything inside this tag is not XML and should be ignored by the parser". The CDATA tag is useful when you want to put some plain text data into an XML document that may have special XML characters in it.

It works like this:

<code>
<![CDATA[
function example($foo, $bar) {
    if ($foo < $bar) {
        return true;
    }
}
]] >
</code>
The code fragment within the CDATA section would stop your XML document from being well formed due to the use of the less than operator. However, since the code is within the CDATA section, the XML parser will ignore the code when parsing the document.

You should place any data that may cause your XML document to become badly formed within a CDATA section. This is most important when generating XML documents dynamically from unverified data that could contain special XML characters.

One thing to remember about CDATA sections; you cannot place a CDATA section inside another CDATA section. That is, the termination string ]]> must not appear anywhere inside the CDATA section, or it will terminate the section and not have the desired effect.

Log in or register to write something here or to contact authors.