EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XML CDATA
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator

XML CDATA

By Priya PedamkarPriya Pedamkar

XML CDATA

Introduction to XML CDATA

XML Character data (CDATA) is defined as Blocks of texts and a type of XML Node recognized by the mark-up languages but are not parsed by the parsers. This is used to solve the inclusion of the mathematical term in the XML document. To pass a math equation <,> CDATA is used to include in the code section. DATA is meant only for the group of text specifying mark-up like characters. The CDATA section includes all the mark-up characters exactly what they are when they are passed it to the application and eliminates nesting. In simple terms, all the entity references and XML tags are excluded by the processor while parsing and treated as character data.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Given below is the CDATA syntax:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,198 ratings)

<!CDATA[
// CDATA CODE with characters
]]>

In the above syntax the first section “<!” starts with the delimiters and ended up with “ ]]> “ just wrapping up with the CDATA tag, the characters in the code sections are treated as only text not as mark-up which may also include special characters. The XML mark-up is embedded which are not interpreted. Any content that is declared inside the<! [CDATA[ and ]]> is ignored by the XML Processor and almost it is equivalent to the comment section in XML.

How CDATA works in XML?

Some reserved characters in PCDATA are not allowed in the XML document during parsing which would give an error in the browser. To get around this we use CDATA section, inherited from SGML. While Executing the file a node with CDATA keyword section is parsed, the parser skips the inner data of Sections. And this special node views like a cross between an element and text also includes opening and closing tags where the text could be multi-lined. The aim objective is to include XML fragments.

Let’s take a sample of an XML document:

[CDATA("most existing feature<BR> ability to inherit") providing XML- data with standards:
<![CDATA[most existing feature<BR> ability to inherit]]>

Here the characters are formatted with CDATA. For CDATA,the character entity is restricted as it generates issues when exporting data with dataset appearing like processing instructions.

The following is invalid as CDATA is nested:

<![CDATA[Nesting is not allowed "]]>" document]]>

So, the above line is written as two parts as follows. This is the exact way to put a section end inside a CDATA section. By which replace]] > in the specified data with]]]] ><! [CDATA [> so that “>” is perfectly away from the brackets (as we broke the bracket sequence). And when the parser looks out for ]]> it is specified as the three-character sequence.

Now let’s see the document which is listed below:

<![CDATA[Nesting is not allowed "]]]]><![CDATA[>" document.]]>

They are helpful when we need to embed another xml document in the existing xml document.

The sample is given below:

<?xml version="1.0" encoding="utf-8"?>
<boarding>
<flight>Iternational</flight>
<Flight name><![CDATA[<?xml version="1.0" encoding="utf-8"?><boarding>…</boarding>]]></Flight name>
</boarding>

Though with certain benefits of CDATA, clearly it has some limitations too, CDATA does go well with html and XHTML. There are few possibilities in losing the contents. Since CDATA cannot be nested section end is made more special. Last point is size of the section that is important while transferring XML file with large amount of information.

Examples of XML CDATA

Given below are the examples mentioned:

Example #1

Enclosing the character entities with CDATA in xml file.

Code:

cdata.xml

<?xml version="1.0" encoding="UTF-8"?>
<SoftwareEngineer>
<empl id="01">
<name>
<projectname> Man-router</projectname>
<Workingdomain> machine learning</Workingdomain>
</name>
<Enddate>
<entities><![CDATA[
This is the local project with the fibre optics.
All the statistical manipulation is performed. Example. '"&<> and submission date 12/12/2020
]]></entities>
</Enddate>
</empl>
</SoftwareEngineer>

So in the Result output, there is a white space after the CDATA sections.

Output:

Xml CDATA 1

Example #2

CDATA with mathematical expression to be included in the XML file.

Code:

cdata.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Displaying the Mathematical function </title>
</head>
<body>
<h1><b> This example Shows how to display a CDATA  when Special characters are included </b></h1>
<div id ="Demo">
<![CDATA[
The statement of the formula includes " if Q(a,b) is a b < c && a > c "
]]>
</div>
</body>
</html>

Output:

mathematical expression

Example #3

HTML file inside CDATA part.

Code:

har.xml

<HTML-Doc>
<tag>
<tname>B</tname>
<description>Bold</description>
<demo><![CDATA[
<B>Bold the letters in the content <EM>Next</EM>part.</B>
]]></demo>
</tag>
<tagtwo>
<tname>Element</tname>
<description>Doc Type Element</description>
<demo><![CDATA[
<HTML>
<HEAD><h1>Sample XML Code</h1></HEAD>
<BODY>
<i>Make a paragraph change</i>
</BODY>/HTML>
]]></demo>
</tagtwo>
<!--more tags to follow...-->
</HTML-Doc>

Output:

HTML file inside

Example #4

Embedding JavaScript or any style sheets in CDATA section.

Probably if suppose there is a situation in including JavaScript in XML code then here is the solution by imposing them in CDATA. The “<” and “>” are likely to be wiped by the equivalents &lt and &gt.

Code:

func.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/TR/xhtml1" xml:lang="en" lang="en">
<head>
<h1>Cdata in Xhtml- demo</h1>
</head>
<body>
<script type="text/JavaScript">
<![CDATA[
function open( x, y, z ) {
if( x< y && y > z ) {
return y--;
}
return 0;
}
]]>
</script>
</body>
</html>

Output:

Embedding JavaScript or any style sheets

Example #5

Code:

func.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8"/>
<title>XHTML Page- CDATA</title>
</head>
<body>
<h2>XHTML Page- CDATA</h2>
<p> I think java  would dominate the Programming Landscape<em>effectively</em>
java.</p>
<p>
java version:
</p>
<p id="1">
<p> I think java  would dominate the Programming Landscape<em>effectively</em>
java.</p>
</p>
<p>
CDATA Sectionpart:
</p>
<p id="2">
well,
<![CDATA[<p> I think java  would dominate the Programming Landscape<em>effectively</em>
java.</p>
]]>
in current trends?
</p>
</body>
</html>

Output:

xml cdata 5

Conclusion

Therefore, in this article, we have seen what is CDATA all about and have analysed how to escape special characters while using in the CDATA section. It has many business impacts when the tags are used improperly, but has good performance when utilized properly. And CDATA is been used when they are need of large amount of texts to appear in the XML documents. The overall summary is while using CDATA to embed mark-up in XML document benefits us from having to encode the data, care should be taken with the effect of the client-server communication.

Recommended Articles

This is a guide to XML CDATA. Here we discuss the introduction to XML CDATA, how it works in XML along with examples respectively. You may also have a look at the following articles to learn more –

  1. XML Versions
  2. XML Commands
  3. XML Features
  4. XML with CSS
Popular Course in this category
XML Training (5 Courses, 6+ Projects)
  5 Online Courses |  6 Hands-n Projects |  40+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more