Open In App

XML - CDATA Sections

Last Updated : 13 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

CDATA sections are a mechanism in XML for handling character data that might otherwise be misinterpreted by the XML parser. CDATA stands for Character Data. These sections include blocks of text within an XML document that the parser should treat literally, without interpreting any characters as XML markup. This is helpful when the text contains characters that would normally have special meaning in XML, such as < (less than sign) > (greater than sign) and & (ampersand).

Syntax:

<![CDATA[  
characters with markup
]]>

where:

  • <![CDATA[: CDATA Start section
  • ]]>: CDATA End section

Rules for CDATA

  • No Nesting: You cannot have CDATA sections nested within other CDATA sections as it will be invalid.
  • No "]]>" Inside: The string sequence "]]>" cannot appear within the CDATA section itself, as it would be misinterpreted as the closing delimiter and can cause parsing errors.

Example: The example below shows an example of CDATA, each character written inside the CDATA section is ignored by the parser. everything between <message> and </message> is treated as character data and not as markup.

XML
<script> 
    /* <![CDATA[ */ 
    let message = "This is a message with < and > symbols"; 
    /* ]]> */ 
    console.log(message); 
</script>

Output:

This is a message with < and > symbols

Next Article

Similar Reads