Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.6. Adding-Editing-Removing nodes

3.6. Adding-Editing-Removing nodes
Last updated: 9 February 2013.

This tutorial will show you how to add new nodes to a JDOM document, how to edit existing nodes and how to remove nodes from a JDOM document.

A node is an instance of the class Element. In fact, everything revolves around the class Element which provides a set of methods to add, edit and remove nodes. As a starting point, the code examples given on this page will read the file hotel.xml shown below. Then the obtained JDOM document will be used to show you how to add, edit, and remove nodes.

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <JACUZZI>3 SEATS</JACUZZI>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>



3.6.1. How to add new nodes to a JDOM document

To add a new element to a JDOM document, you create an instance of the class Element to which you add content by calling one of the methods addContent provided by the class Element. Then you add the newly created element to its parent by calling addContent on the parent. The code below shows how to add a new ROOM element to the JDOM document obtained by reading the file hotel.xml:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndAddNewElement(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Get the root element
               
Element rootElement = document.getRootElement();

               
// Create a new ROOM element
               
Element room = new Element("ROOM");

               
// Set up a new NUMBER element and add it to ROOM
               
Element number = new Element("NUMBER");
                number.addContent
("7");
                room.addContent
(number);

               
// Set up a CATEGORY element and add it to ROOM
               
Element category = new Element("CATEGORY");
                category.addContent
("STANDARD");
                room.addContent
(category);

               
// Set up a BED element and add it to ROOM
               
Element bed = new Element("BED");
                bed.addContent
("DOUBLE SIZE");
                room.addContent
(bed);

               
// Set up a FLOOR element and add it to ROOM
               
Element floor = new Element("FLOOR");
                floor.addContent
("1");
                room.addContent
(floor);

               
// Set up an AIR_CONDITIONING element and add it to ROOM
               
Element airConditioning = new Element("AIR_CONDITIONING");
                airConditioning.addContent
(new Element("TABLE_FANS").addContent("3"));
                room.addContent
(airConditioning);

               
// Add the newly created ROOM element to the root element
               
rootElement.addContent(room);

               
// Output the JDOM document to the standard output
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, System.out);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndAddNewElement();
   
}

}

As you can see, the JDOM API is intuitively simple to use. After reading the file hotel.xml, I called the method getRootElement provided by the class Document in order to get an instance of the root element (HOTEL). In fact, every JDOM document has a root element containing the JDOM tree structure. It is the root element which gives access to the other elements of the tree. Once I got the root element, I created and populated a ROOM element that I added to the root element. Here is the result:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <JACUZZI>3 SEATS</JACUZZI>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>7</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>1</FLOOR>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>3</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>



3.6.2. How to edit existing nodes

You can change the content of an element by calling the method setText or one of the methods setContent provided by the class Element. The following example shows how to change the value of the element FLOOR to 3 for the first ROOM element:

import java.io.File;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndEditElement(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Get the root element
               
Element rootElement = document.getRootElement();

               
// Get the first ROOM element
               
List<Element> children = rootElement.getChildren();
                Element firstRoomElement = children.get
(0);

               
// Change the value of the element FLOOR to 3
               
firstRoomElement.getChild("FLOOR").setText("3");

               
// Output the JDOM document to the standard output
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, System.out);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndEditElement();
   
}

}

Here is the result:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>3</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <JACUZZI>3 SEATS</JACUZZI>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>



3.6.3. How to remove nodes from a JDOM document

To remove elements from a JDOM document, just call one of the methods removeChild, removeChildren, removeContent or detach provided by the class Element.

Alternatively, you can also get the list of children by calling getChildren and remove elements by calling one of the methods provided by the Java collections. The next example shows how to remove the second ROOM element:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndRemoveElement(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Get the root element
               
Element rootElement = document.getRootElement();

               
// Remove the second ROOM element
               
rootElement.getChildren().remove(1);

               
// Output the JDOM document to the standard output
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, System.out);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndRemoveElement();
   
}

}

The output is:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.6. Adding-Editing-Removing nodes
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.7. Creating XML from Java code

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7