Working with JDOM, XPath and XSLT
by Keld H. Hansen
Introduction
When it comes to selecting a format for text data, XML is very
often the natural and obvious choice. There are many advantages
in using XML, for example:
- it's a standardized language.
- it's a language known by all IT professionals.
- with XML, data becomes much more readable to human beings.
- there are tons of tools available for all the things you'd like
to do with XML data.
In this article I'll give some advice on which tools to use for
a given task, and then look further into one of my absolute
favorites, JDOM. In a
previous article I introduced the basic features in JDOM, so
this time I'll look at some of the more advanced features namely
the use of XPath and
XSLT with JDOM.
Which tool should I pick?
If you're new to XML you
might not quite understand the need for having several XML
tools. You might think that an API that allows you to read and
write an XML document would be sufficient. The need for several
tools arises when you realize that XML can be used in a lot of
very different situations. Some examples are:
- configuration files--or parameter files--are often written in XML. They
typically contain technical data for systems like web servers.
- XML files can be used when sending updates of business data to an
application. Think of a list of new customers, orders or exchange rates.
- XML data sometimes have to be persisted on files or in databases, but more
often XML is only a format held in memory.
- XML can be used as a description language for other types of XML
documents. An example is XML schemas that are used to validate other XML
documents.
Let's look at some of the different situations where you need to
read an XML document, for example from a file.
When reading a configuration file, you need to get some or all
of the parameters extracted from the file and then stored
somewhere in your program. Digester is a great tool for this
kind of operation. An introduction to Digester is "Digesting XML documents". Digester is also a natural
choice if you only want to read some of the data in an XML
file.
If you want to read all the data in an XML file and then
continue to do some processing with these data, then you need to
convert the XML into some kind of Java object structure. The
simplest way to do this is to convert into JavaBeans, since
beans are simple to work with. My own favorite tools for
converting between XML files and JavaBeans are Castor and
XMLBeans. Two articles that give you more information are
" Converting XML documents to Java objects with
Castor XML" and "Converting XML to JavaBeans with XMLBeans". XMLBeans
is currently my number one pick since it seems capable of
handling even the most complex XML structures and automatically
packs the generated JavaBeans into a jar-file.
Castor and XMLBeans can be used when your XML files have a
known, fixed structure that doesn't change over time. Castor
and XMLBeans typically take the XML schemas that define the XML
structures and generate beans for you. This is something you'll
have to do manually, so you need an XML format that doesn't
change too often.
If the format of your XML data is not fixed, you need to look
for another tool. JDOM is such a tool, since it'll read any XML
file and produce a tree-like structure in memory. This structure
is very easy to work with for a Java programmer because of its
simple API and its use of well-known Java objects like those
implementing the List interface.
I wrote the introduction "Java and
JDOM: the perfect couple" back in 2002, and in the rest
of the present article I'll dig into some of the more advanced
features in JDOM: XPath and XSLT. I don't assume any deep
knowledge of these standards, since I'll give several simple
examples.
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.
|