How do I convert JDOM Document to String?

This example demonstrate how to convert JDOM Document object to a String using the XMLOutputter.outputString(Document doc) method.

package org.kodejava.example.jdom;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JDOMDocumentToString {
    public static void main(String[] args) {
        Document document = new Document();
        Element root = new Element("rows");

        //
        // Creating a child for the root element. Here we can see how to
        // set the text of an xml element.
        //
        Element child = new Element("row");
        child.addContent(new Element("firstname").setText("Alice"));
        child.addContent(new Element("lastname").setText("Mallory"));
        child.addContent(new Element("address").setText("Sunset Road"));

        //
        // Add the child to the root element and add the root element as
        // the document content.
        //
        root.addContent(child);
        document.setContent(root);

        //
        // Create an XMLOutputter object with pretty formatter. Calling
        // the outputString(Document doc) method convert the document
        // into string data.
        //
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        String xmlString = outputter.outputString(document);
        System.out.println(xmlString);
    }
}

How do I get mixed content of an xml element?

The following examples demonstrate how to read mixed content of an xml element. A mixed content can have more than one type of content such as text (Text), comment (Comment), CDATA (CDATA) or some child elements (Element).

You also see that we can remove the mixed content from the element by calling the remove method of the List just like what we do with a collection of data.

package org.kodejava.example.jdom;

import org.jdom.CDATA;
import org.jdom.Comment;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.EntityRef;
import org.jdom.JDOMException;
import org.jdom.ProcessingInstruction;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class JDOMMixedContent {
    public static void main(String[] args) {
        String xml =
                "" +
                "" +
                "" +
                "User Information" +
                "-data-]]>" +
                "alice" +
                "" +
                "";

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

            //
            // Reading the mixed content of an xml element and iterate
            // the result list. This list object can contains any of the
            // following objects: Comment, Element, CDATA, DocType,
            // ProcessingInstruction, EntityRef and Text.
            //
            List content = data.getContent();

            for (Object o : content) {
                if (o instanceof Comment) {
                    Comment comment = (Comment) o;
                    System.out.println("Comment   = " + comment);
                } else if (o instanceof Element) {
                    Element element = (Element) o;
                    System.out.println("Element   = " + element);
                } else if (o instanceof CDATA) {
                    CDATA cdata = (CDATA) o;
                    System.out.println("CDATA     = " + cdata);
                } else if (o instanceof DocType) {
                    DocType docType = (DocType) o;
                    System.out.println("DocType   = " + docType);
                } else if (o instanceof ProcessingInstruction) {
                    ProcessingInstruction pi = (ProcessingInstruction) o;
                    System.out.println("PI        = " + pi);
                } else if (o instanceof EntityRef) {
                    EntityRef entityRef = (EntityRef) o;
                    System.out.println("EntityRef = " + entityRef);
                } else if (o instanceof Text) {
                    Text text = (Text) o;
                    System.out.println("Text      = " + text);
                }
            }

            //
            // Remove the second mixed content which is the CDATA content.
            //
            content.remove(2);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here are the result of our program:

Comment   = [Comment: <!-- This element contains application data -->]
Text      = [Text: User Information]
CDATA     = [CDATA: <table><tr><td>-data-</td></tr></table>]
Element   = [Element: <field/>]