Home Tutorial Xml Jdom JDOM CDATA Example, Use of append(String str) method of CDATA class.

 
 

Share on Google+Share on Google+
JDOM CDATA Example, Use of append(String str) method of CDATA class.
Posted on: August 26, 2010 at 12:00 AM
Advertisement
In this tutorial, you will see the use of append(String str) method of CDATA class.

JDOM CDATA Example, Use of append(String str) method of CDATA class.

In this tutorial, we will implement append() method of  CDATA class. The CDATA class is available
in org.jdom package. The append( java.lang.String string) method will append character data with
in this CDATA node.

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 ) method of Element class is used to set the content at element.

Code:

Book.xml

<?xml version="1.0" encoding="UTF-8"?>
<BookCatalog>
  <book name="java"></book>
   <book name="c"></book>
</BookCatalog>

AppendCdata .java

package roseindia;
import java.io.File;
import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class AppendCdata {
  public static void main(String[] args) {
    String xml = "Book.xml";
    File file = new File(xml);
    SAXBuilder builder = new SAXBuilder();
    try {
      Document doc = builder.build(file);
      Element rootElement = doc.getRootElement();
      Element empElement = rootElement.getChild("book");
      CDATA cdata = new CDATA(
      "<Detail>java is a programming book.</Detail>.");
      empElement.setContent(cdata);
      cdata.append("It support oops concept.");
XMLOutputter outXML = new XMLOutputter(Format.getPrettyFormat());
      outXML.output(doc, System.out);
      String charData = empElement.getText();
      System.out.println("Charater data : " + charData);
    catch (Exception ex) {
      System.out.println("Exception : " + ex);
    }
  }
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<BookCatalog>
<book name="java">
<![CDATA[<Detail>java is a programming book.</Detail>.It support oops concept.]]></book>
 <book name="c" /> </BookCatalog>
Charater data : <Detail>java is a programming book.</Detail>.It support oops concept.

Download this code

Advertisement

Related Tags for JDOM CDATA Example, Use of append(String str) method of CDATA class.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 26, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com