DM 171: Week Ten - CDATA

CDATA or Character Data

If you tried to use a < (greater than symbol) or & (ampersand) within your XML file text strings between your XML tags, you may have noticed some problems. When an XML parser comes across a <, it automatically thinks that another tag is opening. Similarly, the parser also thinks that an & is always the start of an escape character and looks for the rest of the character and closing semicolon to be happy. Using escape characters for both (&gt; and &amp; respectively) is always an option but sometimes you need another method. This is where the CDATA section is useful.

CDATA Section

When you want the XML parser to leave your text alone, surround the text with a CDATA section. It is a little like comments in an XHTML or CSS file.

The CDATA section starts with: <![CDATA[ and ends with ]]>. All characters within the CDATA section are interpreted as characters and not as XML markup tags or escape characters (entity references).

This can be very helpful when your text string may have a math comparison ( a < b) or is a URL querying a database that contains ampersands.

Some References


A search on CDATA will provide many more references.