Tutorials : Working with JDOM, XPath and XSLT :

JDOM -- finally version 1.0

Just recently--September 9th, 2004--version 1.0 was finally released. This doesn't mean that this was the first release that was stable and useful--it has been so for years. Most open source authors would probably have numbered it 3 or 4, but Jason Hunter has his reasons. If you're curious to know more read Jason's JDOM blog.

To use version 1.0 you should go to the JDOM download page and get the file for your operating system. You then need to extract the jdom.jar file--located in the build folder--and put it in your classpath.

First, a quick recap of the JDOM architecture. JDOM keeps XML elements in the Element object and attributes in the Attribute object:

Figure 1: JDOM Elements and Attributes

The root element is kept in a Document object.

To read an XML file and access the root element you only have to code:

SAXBuilder builder = new SAXBuilder(); // Build a document …
Document doc = builder.build("d:\\ex1.xml"); // … from
a file Element root = doc.getRootElement(); // Get the root

For the XPath examples we need a utility method that can print a List of Elements, including its attributes and child elements. Here are three methods that we can use for this:

Listing 1: The listElements method

private static void listElements(List es, String indent) {
  for (Iterator i = es.iterator(); i.hasNext();) {
    Element e = (Element) i.next();
    listElement(e, indent);
  }
}

private static void listElement(Element e, String indent) {
  System.out.println(indent + "*Element, name:" + 
                     e.getName() + ", text:" + 
                     e.getText().trim());

  //List all attributes
  List as = e.getAttributes();
  listAttributes(as, indent + " ");

  //List all children
  List c = e.getChildren();
  listElements(c, indent + " ");
}

private static void listAttributes(List as, String indent) {
  for (Iterator i = as.iterator(); i.hasNext();) {
    Attribute a = (Attribute) i.next();
    System.out.println(indent + "*Attribute, name:" + 
                       a.getName() + ", value:" + 
                       a.getValue());
  }
}

For our examples we'll use this XML file, dvds.xml:

Listing 2: The dvds.xml file

<?xml version="1.0" encoding="UTF-8"?>
<collection>
<dvd id="A">
  <title>Lord of the Rings: The Fellowship of the Ring</title>
  <length>178</length>
  <actor>Ian Holm</actor>
  <actor>Elijah Wood</actor>
  <actor>Ian McKellen</actor>
</dvd>
<dvd id="B">
  <title>The Matrix</title>
  <length>136</length>
  <actor>Keanu Reeves</actor>
  <actor>Laurence Fishburne</actor>
</dvd>
<dvd id="C">
  <title>Amadeus</title>
  <length>158</length>
  <actor>F. Murray Abraham</actor>
  <actor>Tom Hulce</actor>
  <actor>Elizabeth Berridge</actor>
</dvd>
<dvd id="D">
  <title>Chain Reaction</title>
  <length>106</length>
  <actor>Morgan Freeman</actor>
  <actor>Keanu Reeves</actor>
</dvd>
</collection>

If we parse this file and then use listElement on the root element we'll get this output:

Listing 3: Output from listElements

*Element, name:collection, text:
  *Element, name:dvd, text:
    *Attribute, name:id, value:A
    *Element, name:title, text:Lord of the Rings: The Fellowship of the Ring
    *Element, name:length, text:178
    *Element, name:actor, text:Ian Holm 
    *Element, name:actor, text:Elijah Wood  
    *Element, name:actor, text:Ian McKellen  
  *Element, name:dvd, text:
    *Attribute, name:id, value:B
    *Element, name:title, text:The Matrix
. . .
  *Element, name:dvd, text:
    *Attribute, name:id, value:D
    *Element, name:title, text:Chain Reaction
    *Element, name:length, text:106
    *Element, name:actor, text:Morgan Freeman  
    *Element, name:actor, text:Keanu Reeves  

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.