How do I add and remove elements from XML document in JDOM?

The List returned by the getChildren() method call is a java.util.List. Any modification to the List object immediately reflected in the backing document. This make the manipulation of the XML document easy.

You can also see how we can add a new Element to the document by calling the addContent() method.

package org.kodejava.example.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.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

public class JDOMAddRemoveElement {
    public static void main(String[] args) {
        String xml = "<root>" +
                "   <user name=\"Alice\" age=\"21\" dob=\"20-01-1988\"/>" +
                "   <user name=\"Bob\" age=\"23\" dob=\"01-05-1986\"/>" +
                "</root>";

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

            // Adding a new element to the root of the document using the
            // addContent method.
            document.getRootElement().addContent(
                    new Element("people").setAttribute("id", "1"));
            document.getRootElement().addContent(
                    new Element("people").setAttribute("id", "1"));

            // Add a new element. By adding a new element to the List of
            // children we can modified the xml document. Using 
            // java.util.List makes the modification of XML document 
            // simple and easy.
            List<Element> children = document.getRootElement().getChildren();
            children.add(new Element("user")
                    .setAttribute("name", "Carol")
                    .setAttribute("age", "25")
                    .setAttribute("dob", "06-03-1984"));

            // Add element to the beginning of the xml document.
            children.add(0, new Element("user")
                    .setAttribute("name", "Jimmy")
                    .setAttribute("age", "25")
                    .setAttribute("dob", "16-05-1984"));

            // Remove the fourth element.
            children.remove(3);

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

The result of this code is:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user name="Jimmy" age="25" dob="16-05-1984" />
  <user name="Alice" age="21" dob="20-01-1988" />
  <user name="Bob" age="23" dob="01-05-1986" />
  <people id="1" />
  <user name="Carol" age="25" dob="06-03-1984" />
</root>

Maven Dependencies

<!-- https://mvnrepository.com/artifact/org.jdom/jdom2 -->
<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6</version>
</dependency>

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply