How do I set XML element’s text content?

This example show you how so set text content of XML element. In 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.example.jdom;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.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 automatically escaped so we don't have to use the 
            // &lt; and &gt; 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();
        }
    }
}

How do I get xml element’s text content?

package org.kodejava.example.jdom;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.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();
        }
    }
}