Table of Contents
JDOM is a Java API that provides the user with the ability to create and manipulate XML documents. JDOM provides Java specific XML functionality.
It is assumed that you know how to setup environment variables and install software on your operating system, for a comprehensive guide to doing so for Windows see Configuring A Windows Working Environment, for Unix see Configuring A Unix Working Environment Follow these instructions to install JDOM:
Download and unzip the latest binary distribution of JDOM from http://www.jdom.org/dist/binary/.
Add /path/to/wherever-you-unzipped-jdom/build/jdom.jar to your CLASSPATH environment variable.
The JDOM API is now ready to use. The API docs are situated at /path/to/wherever-you-unzipped-jdom/build/apidocs/, these are the canonical resource for programming with JDOM much as the Java API is the canonical resource for programming with Java in general.
Since JDOM is an inherently specialist tool there are not really any “basics” because most people who use JDOM will be doing so for a specific reason. We will introduce JDOM with two basic examples so that you can get an idea of how to use JDOM. A basic understanding of Java Objects, classes and inheritance is assumed.
Getting XML input with JDOM is extremely simple to do, the following example will illustrate this:
Example 1. JDump
|
The JDOM and Java classes required are imported. | |||
|
The number of command line arguments is checked, if it is zero, a message is produced summarizing usage. | |||
|
A new builder is created to build a JDOM tree. In this case, a SAXBuilder has been used, which builds a JDOM tree with SAX: http://www.saxproject.org/. The other builder available is DOMBuilder which builds a JDOM tree using DOM. It should preferably be used for building from a pre-existing DOM tree because otherwise a DOM tree will be built from the XML file first which is inefficient, SAX should be used instead. JDOM will use the default validating parser, there is another constructor available for specifying a validating parser to use. | |||
|
First of all a new Document is created, which is a JDOM tree representation of an XML tree. builder.build(args[0]) builds a new Document based on the first command line argument, the method used is:
The URL supplied can be a filename. There are many other build methods that build Documents based on other forms of input, the JDOM API lists them all.
If the document is valid, no exception will be thrown and the desired action is to dump the document to stdout; an XMLOutputter Object is created with two parameters, the first specifies the desired indentation width the nested elements should have when output and the second specifies that newlines should be produced where appropriate. output is then called with two parameters, the first specifying the Document to output and the second specifying where to output the Document, in this case, System.out. | |||
|
Two errors may be thrown by the last try statement, the first is a JDOMException, this is thrown by the SAXBuilder Object if it either fails to parse the document correctly or the document does not exist, in either case the error message generated by the object is printed. The second is an IOException this is thrown by the XMLOutputter Object if it encounters errors outputting the file, if this happens the error message generated by the object is printed. |
That's it! It is very simple to do and very intuitive.
The following example may look daunting if you are a Java beginner but study it hard and read the comments and you should be able to understand it. The code generates valid XHTML files, it tries to center the table using two different CSS methods, one of which is deprecated. At the moment there is not a single multi-supported way of centering a table using only CSS, if you know, please tell me. Copy the following code into your favorite text editor, study it a little and try to work out what is going on:
Example 2. XHTML Profile Generator
So, as you can see creating XML documents with JDOM is very simple and intuitive.
![]() | Note |
|---|---|
We have only touched on the features available using JDOM. JDOM has numerous methods for navigating the JDOM tree such as getRootElement() which gets the root element of a Document, getChildren() which returns a (live, aka modifiable)List of all the child elements nested directly(one level deep) within an Element and getNameSpace() which will return the Element's namespace. There are loads more, with a bit of recursion JDOM tree navigation and manipulation can be done easily and efficiently. The details are all there in the JDOM API | |
http://www.jdom.org/docs/apidocs/
The JDOM API
Copyright © 2002 Jason Hunter,
Brett McLaughlin. All Rights Reserved.
http://www.servlets.com/speaking/jdom-javaone.pdf
JDOM Makes XML Easy
Jason Hunter
Co-Creator
JDOM Project
http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html
Easy Java/XML integration with JDOM, Part 1
By Jason Hunter and Brett McLaughlin