XML - CDATA Sections



In this chapter, we will discuss XML CDATA section. The term CDATA means, Character Data. CDATA is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup.

The predefined entities such as <, >, and & require typing and are generally difficult to read in the markup. In such cases, CDATA section can be used. By using CDATA section, you are commanding the parser that the particular section of the document contains no markup and should be treated as regular text.

Syntax

Following is the syntax for CDATA section −

<![CDATA[
   characters with markup
]]>

The above syntax is composed of three sections −

  • CDATA Start section − CDATA begins with the nine-character delimiter <![CDATA[

  • CDATA End section − CDATA section ends with ]]> delimiter.

  • CData section − Characters between these two enclosures are interpreted as characters, and not as markup. This section may contain markup characters (<, >, and &), but they are ignored by the XML processor.

Example

The following markup code shows an example of CDATA. Here, each character written inside the CDATA section is ignored by the parser.

<script>
   <![CDATA[
      <message> Welcome to TutorialsPoint </message>
   ]] >
</script >

In the above syntax, everything between <message> and </message> is treated as character data and not as markup.

CDATA Rules

The given rules are required to be followed for XML CDATA −

  • CDATA cannot contain the string "]]>" anywhere in the XML document.
  • Nesting is not allowed in CDATA section.
Advertisements