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>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024