|
Using XML with Java (and being practical about it)
Its always hard to decide which is the most overhyped "technology" in the
IT world. Fairly high up on my shortlist of candidates is XML. Some time just
before the year 2000, various programmers around the world began to be assigned
tasks that went something like: "I want you to write a routine to read a file
in this format":
<book id="776">
<title>How to make simple things complicated</title>
<coverType>hard</coverType>
</book>
Now you might have thought that said programmers would all have turned
to their bosses and said "OK, I'll have something knocked up in half an hour" and
that would have been the end of it.
But at some point, somebody somewhere told their Project Manager
that this was a Complex Programming Task. The Project Manager, already overstretched with
the task of checking that Philips B42 light bulbs were Millennium Compliant,
took their word for it. Before anyone could save the world from the impending chaos,
the relevant Opportunity for Pointless IT Acronyms forms
had been dutifully submitted to the Marketing Department in triplicate.
And thus was born the mind-boggling and
largely pointless array of "XML technologies" that we have today.
Or at least, that's my theory.
So what's XML and what's the big fuss?
XML is a file format designed to provide a standard, platform-independent,
human-readable way of storing structured data. And in fairness, that is something
that the world needs. I'm also being a little unfair in my description above, because
the XML standard actually provides a few other sometimes uesful features such as document
validation. But the bottom line is that the most common thing that most people want
to do with XML is parse a simple document in the above format.
The problem then is that the standard "technology" that has emerged for
dealing with XML generally does it in a slightly idiotic and overcomplicated way.
Sadly, Java is not much of an exception to this.
Simple XML parsing
In this tutorial I'm going to focus largely on parsing XML using the XPath
facilities added in Java 5. XPath, which we'll introduce as we go along, is
a syntax for referring to parts of an XML document via paths, a bit like a filing system.
Although the API for using XPath in Java is somewhat clumsy, in my opinion,
it is still the most practical of the various impractical methods available
for reading XML.
So, let's get started with parsing XML in Java using XPath.
Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey.
Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles,
or if you simply have a programming question that you would like to see answered on this site. Most topics
will be considered. But in particular, the site aims to provide tutorials and information on topics that
aren't well covered elsewhere, or on Java performance information that is poorly described or understood.
Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2009. All rights reserved.
|