Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.8. Retrieving nodes content

3.8. Retrieving nodes content
Last updated: 9 February 2013.

This tutorial will give you tips in retrieving the content of a JDOM document.

You can easily access the nodes of a JDOM tree and retrieve their content by using the methods provided by the class Element. For example, suppose that you are working with the file hotel.xml shown below and you want to retrieve the content of the TABLE_FANS node belonging to the first ROOM node.

   <?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>

After parsing the XML file hotel.xml shown above, the code below uses the methods getChildren, getChild and getText to retrieve the content of the TABLE_FANS node:

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

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


public final class JDOMDemo {


   
private void retrieveNodeContent(){
         
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);

               
// Get the TABLE_FANS content
               
Element tableFansElement = firstRoomElement.getChild("AIR_CONDITIONING").getChild("TABLE_FANS");
               
int tableFansNumber = Integer.parseInt(tableFansElement.getText());

               
// Do something with tableFansNumber
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


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

}

To take another example, let's say you want to count the number of BED nodes contained in the file hotel.xml. To achieve that, you need to walk through the JDOM document in order to count the BED occurrences. The best way to do that is by using an iterator. Here is the code:

import java.io.File;
import java.util.Iterator;

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


public final class JDOMDemo {


   
private void countNumberOfBeds(){
         
int numberOfBeds = 0;

         
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();

               
// Walk through the document and count the BED occurrences
               
Iterator<Element> iterator = rootElement.getChildren().iterator();
               
while(iterator.hasNext()){
                     
Element roomElement = iterator.next();
                      numberOfBeds += roomElement.getChildren
("BED").size();
               
}

               
// Do something with numberOfBeds
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


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

}

In this tutorial, to retrieve the nodes content, I have used methods provided by the class Element together with methods provided by the classes java.util.List and java.util.Iterator. However, another more advanced way to retrieve content is using an XPath expression through JDOM's XPathFactory. For more information, please refer to the JDOM javadoc.


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.8. Retrieving nodes content
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.9. Moving nodes

Copyright © 2013. 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