XML - CDATA

What CDATA Means

  • CDATA = Character Data.

  • A CDATA section tells the XML parser:

    “Treat everything inside here as plain text, don’t parse it as XML markup.”


Syntax

<![CDATA[
   raw text goes here
]]>

Why It’s Needed

Normally, XML parsers interpret <, >, and & as special markup characters.
If you want to include raw text that contains these characters, you wrap it in CDATA.


Example

Without CDATA (problematic):

<code>
   if (a < b && b > c) { ... }
</code>
  • Here, < b and && might confuse the parser.

With CDATA (safe):

<code>
   <![CDATA[
      if (a < b && b > c) { ... }
   ]]>
</code>
  • Inside CDATA, < and & are treated as literal text, not markup.


Important Rules

  1. CDATA can contain any text, but cannot include ]]> (since that ends the CDATA block).

  2. CDATA does not prevent the XML parser from reading the text — it just prevents parsing of markup.

  3. Equivalent to writing escaped characters (&lt;, &amp;) — CDATA is just more convenient.


In Short

  • CDATA Section = a way to include raw text in XML without escaping special characters.

  • Useful for embedding code, scripts, or text with symbols like <, >, and &.