The CDATA Tag

The Problem: Illegal Characters

When writing your XML documents, you must be aware that it is possible to confuse a parser, or even break your document, by including illegal characters.

For example, the following document would cause a problem:

<example>The symbol < denotes "less than"</example>

In this case, the XML parser sees the less than symbol in the text as an opening XML tag.

The Solution: CDATA

To include special characters, or preserve special formatting, you can use a CDATA tag.

<![CDATA[...]]>
 
This tag wraps around the special content and tells the XML parser to consider everything therein as plain text. This is particularly useful when you want to include HTML or formatted text inside your XML document, because you don’t want the HTML tags to be interpreted as nested XML tags.
 
So the above example becomes:
 
<example><![CDATA[The symbol < denotes "less than"]]></example>

Using special characters without CDATA

However, if you want to use a special character in your XML (say for example in an attribute value where no CDATA exists), you can do so using its HTML character entity references.

&name;

So, to insert the ampersand symbol '&', you use:

&amp;

And in XML:

<title txt='Name &amp; Surname'>

For a list of all the HTML character entity references, click here.

Post A Comment

Anti-Spam Quiz: