dom4j

dom4j
began as a fork of the JDOM project by a developer,
James Strachan,
who disagreed with several aspects of the JDOM architecture
including the decision to use classes instead of
interfaces. dom4j has since gone its own way, and features a
very different approach to a pure-Java, tree-based API
than either DOM or JDOM. Notable unique features
in dom4j include integrated XPath and XSLT support
and optional DOM compatibility. (e.g., you can wrap
a dom4j Element
inside a DOM Element.)

Example 5.8 shows the
dom4j version of our standard example. It’s structured
pretty much like the previous two tree based APIs.
First a Document object representing
the XML-RPC request is
constructed in memory; then serialized onto the
URLConnection’s output stream. Finally,
the response document is parsed to find the desired value.

In dom4j the request Document
object is initially created by the DocumentHelper.createDocument()
factory method. The contents of the document are formed
at the same
time they’re added to their parent so that no nodes
are ever orphaned from the tree.
The serialization to the server is performed by
the XMLWriter class.

Once the response has been received,
dom4j parses it into a Document
object using the SAXReader class
that connects dom4j to an underlying
parser such as Crimson or Xerces.
XPath is used to
extract the double
element from this document.
If you’re comfortable with XPath, this is a very useful
and convenient feature.