How do I build xml CDATA sections?

Category: org.jdom, viewed: 6230 time(s).

This example demonstrate how to add CDATA section into an xml document. A CDATA section indicates a block that shouldn't be parsed. To build a CDATA section just wrap the string with a CDATA object.

package org.kodejava.example.jdom;

import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Element;
import org.jdom.CDATA;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;

import java.io.StringReader;
import java.io.IOException;

public class JDOMBuildCDATASection {
    public static void main(String[] args) {
        String xml =
                "<root>" +
                "   <comments>" +
                "       <comment></comment>" +
                "   </comments>" +
                "</root>";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(new StringReader(xml));
            Element root = document.getRootElement();
            Element comments = root.getChild("comments");

            Element comment = comments.getChild("comment");

            //
            // Using the setContent and addContent to add CDATA section into
            // the xml element.
            //
            comment.setContent(new CDATA("<b>This is a bold string</b>."));
            comment.addContent(new CDATA("<i>And this an italic string</i>."));

            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(document, System.out);

            //
            // Reading a CDATA section is simply done by calling the getText
            // method. It doesn't care if it was a simple string or a CDATA
            // section, it will just return the content as string.
            //
            String text = comment.getText();
            System.out.println("Text = " + text);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats