How to escape the XML CData section ending sequence `]]>`?
How to Escape the XML CDATA Section Ending Sequence
In XML, CDATA sections are used to include text that should not be parsed by the XML parser. However, the string ]]> cannot appear inside a CDATA section because it signals the end of the section.
The Workaround
To include ]]> inside your data, you must split the CDATA section:
<![CDATA[some data ]]]]><![CDATA[> more data]]>
By ending the first CDATA, adding an escaped ] (as ]]>), and then starting a new CDATA for the >, you can safely represent the sequence.
XML in 2026
- JSON Dominance: While XML is still used in enterprise systems (SOAP, SVG, Office formats), JSON has become the dominant format for web APIs and configuration.
- Safety: When generating XML in 2026, developers almost always use a dedicated library (like
lxmlin Python orJAXBin Java) that handles these escaping rules automatically, preventing injection vulnerabilities.