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

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.