How do I read an XML document using JDOM?

This example provide a small code snippet on how to use JDOM to parse an XML document, iterates each of the element and read the element or attributes values. To play with this example you have to have the JDOM library. You can download it from JDOM website.

package org.kodejava.jdom;

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

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ReadXmlDocument {
    public static void main(String[] args) {
        // Our imaginary xml file to be processed in this example.
        String data = """
                <root>
                    <row>
                        <column name="username" length="16">admin</column>
                        <column name="password" length="128">secret</column>
                    </row>
                    <row>
                        <column name="username" length="16">jdoe</column>
                        <column name="password" length="128">password</column>
                    </row>
                </root>""";

        // Create an instance of SAXBuilder
        SAXBuilder builder = new SAXBuilder();
        try {
            // Tell the SAXBuilder to build the Document object from the
            // InputStream supplied.
            Document document = builder.build(
                    new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));

            // Get our xml document root element which equals to the
            // <root> tag in the xml document.
            Element root = document.getRootElement();

            // Get all the children named with <row> in the document.
            // The method return the children as a java.util.List
            // object.
            List<Element> rows = root.getChildren("row");
            for (Element row : rows) {
                // Convert each row to an Element object and get its
                // children which will return a collection of <column>
                // elements. When we have to column row we can read
                // the attribute value (name, length) as defined above
                // and also read its value (between the <column></column>
                // tag) by calling the getText() method.
                //
                // The getAttribute() method also provide a handy method
                // if we want to convert the attribute value to a correct
                // org.kodejava.data type, in the example we read the length attribute
                // value as an integer.
                List<Element> columns = row.getChildren("column");
                for (Element column : columns) {
                    String name = column.getAttribute("name").getValue();
                    String value = column.getText();
                    int length = column.getAttribute("length").getIntValue();

                    System.out.println("name = " + name);
                    System.out.println("value = " + value);
                    System.out.println("length = " + length);
                }
            }
        } catch (Exception 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.