Home » XML

CDATA (Character Data) in XML

In this article, we are going to learn about the CDATA (Character Data) in XML - its syntax, characteristics and example.
Submitted by Akash Kumar, on November 07, 2018

Introduction

The Term CDATA basically refers to Character Data. In XML it is basically a block of texts or sentences that are not parsed by the parser and are treated as regular English text. It is difficult to read predefined entities such as <(),&(&) in the markup. For such situation, the concept of CDATA section introduced. In this section basically, we are commanding the parser that this particular section of XML document doesn’t contain markup and consider them as normal plain text.

Syntax

Basic syntax of CDATA section as follows:

    <![CDATA[
        code  with markup
    ]]>

Characteristics

CDATA section basically composed of three section these are as follows:

  1. CDATA Start section: CDATA starts with nine character delimiter that is <![CDATA[.
  2. CDATA End section: CDATA basically ends with three character delimiter that is ]]>.
  3. CData section: Characters written between Cdata Start section and Cdata End Section will be treated as characters, not as markup.

In this section, we basically deal with characters such as (, and &) but these characters are ignored by the processor of XML.

CDATA Rules

There are some basic rules to write CDATA section

  1. CDATA cannot contain the string ]]< anywhere in the XML document.
  2. We cannot put CDATA section under another CDATA section in other words we can say that nesting is not allowed in CDATA.

Usefulness of CDATA

These sections are useful when we cannot write XML code within the XML document.

For example, if a user wants to write a book with XSL which have an explanation about the use of an XML application, the XML markup which is going to appear in the book will be going to write in the source file in a CDATA section which will be ignored by the processor of XML.

Examples of CDATA

Following is an example of CDATA here each character written inside CDATA section is ignored by parser.

<![CDATA[
	IncludeHelp!!!
]] >

In the above example IncludeHelp!!! which is written inside the is treated as character not as markup.

Lets try to understand this using following examples. The following example basically stores the student information that is name.

<?xml version="1.0" encoding="UTF-8"?>
<student>
	<stu_info rollno="7">
		<name>
			<firstname>Akash</firstname>
			<lastname>Sharma</lastname>
		</name>
		<contact_info>
			<entities>
				<![CDATA[
					This is the CDATA area.
					We are basically storing special character that are recognize.
				]]>
			</entities>
		</contact_info>
	</stu_info>
</student>


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.