What is CDATA in HTML?


The full form of CDATA is Character Data, it is one of the sections in XML which is used to interpret character data. It treats the text data as raw text in character format. The Tags inside a CDATA section are not treated as markup and entities are also not expanded.

CDATA section is recognized by the delimiter "]]>" - that indicates the end of the CDATA section. CDATA sections are not nested.

Syntax

The usage/syntax of CDATA used in XML is −

syntax with example:- <![CDATA[ message part here. ]]>

Example

Following is the XML which demonstrates the usage of CDATA −

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<!--Details of Employees-->
<document>
   <employee category="permanent" gender="male" group="a">
      <name>
         <lastname>Gable</lastname>
         <firstname>Clark</firstname>
      </name>
      <hiredate>October 25, 2005</hiredate>
      <!--Project Details of Employees-->
      <projects>
         <project>
            <product>Keyboard</product>
            <id> 555</id>
            <price><$129.00</price>
         </project>
         <project>
            <product>Mouse</product>
            <id> 666</id>
            <price>>$25.00</price>
         </project>
      </projects>
      <![CDATA[
         <messages> Hope </messages>
      ]]>
      <![CDATA[
         <message> Welcome </message>
      ]]>
   </employee>
</document>

Output

Following is the output when sample.xml executes −

CDATA sections are not used within HTML they are considered as comments and not displayed.

Let us discuss more about CDATA

  • CDATA is the alternative way to arrange text in an XML document.

  • XML treats the block of text as character data.

  • The CDATA section includes data in between the string treats as pure textual data by XML parser.

  • For command the parser that particular part of data contains no markup characters, CDATA section is used.

  • In this section all data treats as regular text

  • In CDATA section, the block of text is not parsed by XML parser.

  • CDATA sections are used to overcome the difficulty in reading character and entity references.

  • CDATA section is used whenever there is a need to include large amount of text in an XML document

  • In XML Document, the CDATA section can be placed anywhere, it never be nested and it cannot be empty.

The object properties of CDATA section are as follows −

  • data − It is used to return or set the text of this node

  • length − It is used to return the length of CDATA section

The object properties of CDATA section are as follows −

Property Name

Description

Syntax

appendData()

This method is used to append the data to node.

CDATANode.appendData(string)

deleteData()

This method is used to delete the data from node.

y.deleteData(0, 5);

insertData()

It is used to insert data into node.

insertData(start,string)

replaceData()

It is used to replace the data from node.

CDATANode.replaceData(start,length,string)

splitText()

This method is used to split the CDATA node into two nodes

replaceData(offset)

substringData()

This method is used to extract data from node.

CDATANode.substringData(start,length)

Example

Consider another example to know more about CDATA -

<?xml version="1.0" encoding="UTF-8"?>
<Softwareproduct>
   <empl id="01">
      <name>
         <projectname> Alaxa</projectname>
         <Workingdomain> Machine learning</Workingdomain>
      </name>
      <Enddate>
         <entities>
            <![CDATA[This is the project on Alexa by using the domain Machine learning. All the statistical manipulation is performed. Example. '"&<> and submission date 20/06/2024]]>
         </entities>
      </Enddate>
   </empl>
</Softwareproduct>

Output

Following is the output when sample.xml executes -

Updated on: 04-Oct-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements