- Tech know how online

Java document object model (JDOM)

Java Document Object Model (JDOM) is a Java programmable interface(Application Programming Interface, API) for handling XML documents. JDOM is also implemented in Java and is available as open source software at the link below. JDOM was developed specifically for Java, and is thus in contrast to the platform-independent interfaces Document Object Model (DOM) and Simple API for XML (SAX).

However, JDOM offers better performance as well as better memory usage than the original Document Object Model (DOM). JDOM is not a wrapper for the Document Object Model (DOM) specified by the World Wide Web Consortium (W3C). JDOM is rather a self-sufficient class library that maps a separate class with the corresponding methods for each XML node of the node tree. An alternative to JDOM is dom4j - an API that is also available as open source and offers a wide range of functions. JDOM is based on an initiative by Jason Hunter and Brett Laughlin, so JDOM is neither a specified standard of the W3C nor part of the Java Development Kit (JDK). The developers of Java - the company Sun - offers with JAXP its own Java interface to XML.

JDOM as an Open Source API

JDOM is an open source API for the performant reading, writing as well as the modification of XML documents. For the construction of JDOM models, JDOM does not implement its own parser, but uses an external parser to read the XML document. Compared to the Java package for the DOM (Document Object Model) provided by the W3C, JDOM does not use a node object, and thus the tree to be built from an XML document for its processing is not mapped as an ordered set of XML nodes. JDOM fully supports the mechanisms known from Java, such as inheritance and method overloading, and also makes intensive use of Java's so-called Collection API, which works with containers for objects.

The JDOM package available at the link below includes:

  • org.jdom.adapters,
  • org.jdom.filter,
  • org.jdom.input,
  • org.jdom.output,
  • org.jdom.transform,
  • org.jdom.xpath.
With this set, a number of capabilities of the package also become clear. JDOM maps the XML document to a Java object tree and is based on classes that ultimately map the document in memory. The package org.jdom contains the classes Attribute, Document, CDARA, Comment, DocType, Element and Text. In addition, there are the classes that define an XML namespace or an XML entity reference, as well as the class Verifier, which checks an XML document to see if it is well-formed and valid. And the instances of these classes, according to the object-oriented definition, are the objects that make up a tree.

While the input package supports both a DOM and a SAX parser, the output package consists of so-called helper classes for transforming JDOM documents into another representation of an XML document. For example, a JDOM document can be transformed into a DOM document. The adapters package supports already existing implementations, e.g. adapter classes for Sun JAXP, Oracle, Apache Xerces or Apache Crimson parsers. You can also define your own adapter classes. The filter package organizes the visibility of all JDOM objects in a tree and the selection of element objects. The transform package realizes wrappers for Sun's own JAXP. And finally, the xpath package provides the possibility to make requests to an object tree in JDOM using the XPath language. Methods known from DOM like getElementById() can be implemented in their own version.

JDOM implementations for XML documents

Example of a JDOM conversion

Example of a JDOM conversion

The JDOM implementation faces standardized interfaces like DOM and SAX for processing XML documents. On the other hand, dom4j and JAXP offer possibilities - also on the Java level - for the efficient handling of XML documents. JAXP in particular assigns the wide range of technologies such as DOM, SAX, SAX, XLST and XPath to an overlying abstraction layer with convenient Java interfaces. This also avoids the disadvantage of using external parsers directly and the dependency on specific classes that can be derived from this. Ultimately, however, criteria such as the size, source and structure of the XML documents as well as their frequency and scope of processing will be decisive for the use of a particular interface or tool.

Informationen zum Artikel
Englisch: Java document object model - JDOM
Updated at: 29.10.2013
#Words: 626
Translations: DE