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.

 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