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.jdom;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.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);
    }
}

The result of our code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<rows>
  <row>
    <firstname>Alice</firstname>
    <lastname>Mallory</lastname>
    <address>Sunset Road</address>
  </row>
</rows>

Maven Dependencies

<!-- https://search.maven.org/remotecontent?filepath=org/jdom/jdom2/2.0.6.1/jdom2-2.0.6.1.jar -->
<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

How do I get mixed content of an XML element in JDOM?

The following example demonstrates 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.jdom;

import org.jdom2.*;
import org.jdom2.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 = """
                <root> \
                    <data> \
                        <!-- This element contains application data --> \
                        User Information \
                        <![CDATA[<table><tr><td>-data-</td></tr></table>]]> \
                        <field name="username">alice</field> \
                    </data> \
                </root>""";

        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 contain any of the
            // following objects: Comment, Element, CDATA, DocType,
            // ProcessingInstruction, EntityRef and Text.
            List<Content> contents = data.getContent();

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

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

Here are the result of our program:

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

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

How do I build XML CDATA sections in JDOM?

This example demonstrates 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.jdom;

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

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

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 | IOException e) {
            e.printStackTrace();
        }
    }
}

The output of the code snippet above:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <comments>
    <comment><![CDATA[<b>This is a bold string</b>.]]><![CDATA[<i>And this an italic string</i>.]]></comment>
  </comments>
</root>
Text = <b>This is a bold string</b>.<i>And this an italic string</i>.

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

How do I set XML element’s text content in JDOM?

This example show you how so set text content of XML element. Using JDOM we can easily insert text such as HTML tags without worrying about escaping the tags. JDOM will automatically do this conversion.

package org.kodejava.jdom;

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

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

public class JDOMSetTextContent {
    public static void main(String[] args) {
        String xml = """
                <root>
                    <description>
                    </description>
                </root>""";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(new StringReader(xml));

            Element root = document.getRootElement();
            Element description = root.getChild("description");

            // Adding a text content to the description element. The string
            // will be escaped automatically, so we don't have to use the 
            // < and > symbol.
            description.setText("This is an <strong>IMPORTANT</strong> " +
                    "description");

            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(document, System.out);
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central

How do I get XML element’s text content in JDOM?

package org.kodejava.jdom;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

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

public class JDOMElementTextContent {
    public static void main(String[] args) {
        String xml = """
                <root>
                    <paragraph>
                Lorem ipsum dolor sit amet, consectetur adipisicing \
                elit, sed do eiusmod tempor incididunt ut labore et \
                dolore magna aliqua. Ut enim ad minim veniam, quis \
                nostrud exercitation ullamco laboris nisi ut aliquip \
                ex ea commodo consequat. Duis aute irure dolor in \
                reprehenderit in voluptate velit esse cillum dolore \
                eu fugiat nulla pariatur. Excepteur sint occaecat \
                cupidatat non proident, sunt in culpa qui officia \
                deserunt mollit anim id est laborum.
                    </paragraph>
                </root>""";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(new StringReader(xml));

            Element root = document.getRootElement();
            Element paragraph = root.getChild("paragraph");

            // Returns the textual content directly held under this element as 
            // a string. This includes all text within this single element, 
            // including whitespace and CDATA sections if they exist.
            String content = paragraph.getText();
            System.out.println("content = " + content);

            // Returns the textual content of this element with all surrounding
            // whitespace removed. If no textual value exists for the element,
            // or if only whitespace exists, the empty string is returned.
            String contentTrimmed = paragraph.getTextTrim();
            System.out.println("contentTrimmed = " + contentTrimmed);

            // Returns the textual content of this element with all surrounding
            // whitespace removed and internal whitespace normalized to a single
            // space. If no textual value exists for the element, or if only
            // whitespace exists, the empty string is returned.
            String contentNormalize = paragraph.getTextNormalize();
            System.out.println("contentNormalize = " + contentNormalize);
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6.1</version>
</dependency>

Maven Central