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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
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%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs