In this tutorial, we will see how to add CDATA section in xml document. In xml document, CDATA section is a part of element content that is like as character data. Text inside this section will be ignored by the parser.
In this example, we can create a CDATA node with the help CDATA class.
The CDATA(java.lang.String string) constructor of CDATA class crates a CDATA new node with a string.
The setContent(Content content ) and addContent(Content content) method of Element class is used to set and add the content at element.
Code:company.xml
<?xml version="1.0" encoding="UTF-8"?>
|
CdataSection.java
package roseindia;
|
|
<?xml version="1.0" encoding="UTF-8"?> <Company> <Employee> <name>xyz</name> <Detail><![CDATA[<info>Working in RoseIndia.</info>.]]><![CDATA[<work>Programmer</work>.]]> </Detail> <Phoneno>0808</Phoneno> <CompanyName>RoseIndia.net</CompanyName> </Employee> </Company> Charater data : <info>Working in RoseIndia.</info>.<work>Programmer</work>. |