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 :

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.

 BlackBerry Application Development Resources
 Microsoft Visual Studio 2010 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%.

ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow
Facebook Makes Major PHP Push With HipHop
Free Ride Over for Microsoft Azure Users
Drupal Opens the Garden to Boost CMS
Oracle Talks Plans for Linux, Solaris
Azure Makes Cloud Computing Innovation Safe
Red Hat's JBoss Looks Ahead
Microsoft Readies Two Windows Phone Systems?

Apple Surveying iPhone Developers? Happiness With The App Store
HTML 5 Leaves Client Storage Open to Web Attacks
Basic Market Forecasting with Encog Neural Networks
Location-Aware App Review
The Future of Web Content -- HTML5, Flash, and Mobile Apps
Moonlight 3.0 Preview Offered For Rich Internet Apps on Linux and Unix
Why a Moderator is Key in the Engineering Review Process, Part II
Windows 7 Features Your Clients Will Need on Day One
What Your Clients Will Ask About Windows 7
Melissa Data Helps Developers Improve the Quality of Business Data

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


The Network for Technology Professionals

Search:

About Internet.com

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