Tutorials : Working with JDOM, XPath and XSLT :

XPath

XPath is used for identifying or extracting parts of an XML structure. You could say that XPath is to XML what SQL is to relation databases. To learn about XPath you could start by checking out the pages on w3schools.

Here's a couple of examples using the DVD xml file above:

Table 1: XPath examples
  xxx XPath expression
a Get all dvds /collection/dvd
b Get the dvd with title "The Matrix" /collection/dvd[title="The Matrix"]
c Get the length of "The Matrix" /collection/dvd[title="The Matrix"]/length
d Get the dvd with id="B" /collection/dvd[@id="B"]
e Get the dvds where Keanu Reeves is one of the actors /collection/dvd[actor="Keanu Reeves"]
f Get all id attributes /collection/dvd/@id
g Get the title of dvd with id="B" string(/collection/dvd[@id="B"]/title)
h Get the last dvd /collection/dvd[last()]

There are many more possibilities with XPath, and if you want to learn more you could check out W3C's web site for the full story. If you like to experiment, XPath Explorer is a nice tool for trying out things. It can be used as a stand-alone Java program, and also as an Eclipse and NetBeans plugin. Here's a screen dump where the dvds.xml file is being analyzed:

Figure 2: XPath Explorer

JDOM and XPath

JDOM supports XPath through the org.jdom.xpath.XPath class and the Jaxen API. Jaxen can be downloaded from http://jaxen.org/releases.html. I used version 1.0-FCS for the examples. Extract the jar-files from the download--you'll need at least jaxen- core.jar, jaxen-jdom.jar and saxpath.jar

It's simple to work with XPath in JDOM. Example "a" from the table above is solved by these lines:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build("dvds.xml");
XPath x      = XPath.newInstance("/collection/dvd");
List list    = x.selectNodes(doc);
listElements(list, "");

The output is the same as in Listing 3, except for the first line with the collection element which is left out.

The objects returned in the List object are typically Elements or Attributes, but may also be Text or primitive types, depending on the XPath expression you use. Example "f" above will for example return Attributes:

XPath x   = XPath.newInstance("/collection/dvd/@id");
List list = x.selectNodes(doc);
listAttributes(list, "");

The output is this:

*Attribute, name:id, value:A
*Attribute, name:id, value:B
*Attribute, name:id, value:C
*Attribute, name:id, value:D

Example "g" will return a String:  

XPath x  = XPath.newInstance
("string(/collection/dvd[@id='B']/title)");
String s = (String)x.selectSingleNode(doc);
System.out.println(s);

If you know that an expression will return only one object you may use the selectSingleNode method:

x = XPath.newInstance("//collection/dvd[@id='B']");
Element e = (Element)x.selectSingleNode(doc);
listElement(e, "");

The output will be the following:

  *Element, name:dvd, text:
  *Attribute, name:id, value:B
  *Element, name:title, text:The Matrix
  *Element, name:length, text:136
  *Element, name:actor, text:Keanu Reeves  
  *Element, name:actor, text:Laurence Fishburne  

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.