XML CDATA Sections

Embed raw text in XML without escaping — syntax, use cases, and limits.

Format, minify, or validate XML containing CDATA sections with the free XML Formatter.

What Is a CDATA Section?

CDATA stands for Character Data. A CDATA section is a special XML construct that tells the parser to treat everything inside it as raw text — not as markup. This lets you include characters like < and & without escaping them.

Normally, these characters must be written as &lt; and &amp;. Inside a CDATA section, you can write them literally:

<description><![CDATA[Price: <$10 & free shipping]]></description>

The parser delivers the text content as Price: <$10 & free shipping — exactly as written, no escaping needed.

CDATA Syntax

A CDATA section has three parts:

<![CDATA[  ...content...  ]]>

CDATA sections can appear inside element content — never inside a tag name, attribute, or processing instruction. A CDATA section is equivalent to the escaped version of the same text; the XML data model treats them identically.

When to Use CDATA vs Escaping

Both approaches produce identical parsed output. Choose based on readability and context:

Use CDATA when the content contains many special characters
Embedding HTML snippets, SQL queries, code fragments, or regex patterns in XML is much cleaner in a CDATA section than escaping every <, >, and &.
Use escaping when the content has few special characters
A single ampersand in a company name (AT&amp;T) is cleaner escaped than wrapped in a CDATA section.
Use escaping in attribute values
CDATA sections cannot appear inside attribute values — only entity references work there: title="a &amp; b".

The CDATA Nesting Gotcha

CDATA sections cannot be nested, and the closing delimiter ]]> cannot appear inside a CDATA section. If your content contains the string ]]>, you must split the section:

<!-- INVALID: ]]> inside CDATA terminates it early -->
<![CDATA[a]]>b]]>

<!-- Valid: split at the delimiter -->
<![CDATA[a]]]]><![CDATA[>b]]>

This edge case is rare in practice — it only occurs when embedding CDATA-containing XML inside another XML document.

CDATA in Real-World XML

Common places you will encounter CDATA sections:

How This Formatter Handles CDATA

The XML Formatter preserves CDATA sections during Format and Minify operations. The content between <![CDATA[ and ]]> is never modified or re-escaped — only surrounding whitespace outside the section may be adjusted during formatting.

The Validate function treats CDATA sections as plain text and does not report them as errors. The XML→JSON conversion delivers the raw text content of a CDATA section as the string value of the corresponding JSON key — the CDATA wrapper is transparent to the caller.