advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs