3.3. What is JDOM?
Last updated: 2 January 2013.
JDOM is an open source Java library which provides a very intuitive XML processing API. Using JDOM, you can read, write and manipulate XML documents very easily. Because it is a third-party library that is not integrated into the JDK, it is supplied as a JAR file that you have to download and include in your classpath.
You might be wondering why use JDOM instead of another API. In fact, there is a number of APIs specially dedicated to XML processing in Java. Some of the best known ones are JDOM, JAXP, DOM and SAX. Of those, JDOM is the most user-friendly for common XML processing tasks, which is why it is widely used in the Java community.
DOM (Document Object Model) and SAX (Simple API for XML) are parsing APIs. More precisely, they are specifications (they provide a set of interfaces to implement, just like the JDBC API is a set of interfaces that are implemented by a driver).
DOM was originally designed to be language independent. Since it was not designed for Java, DOM is more difficult to use than JDOM which was specifically made for Java. In particular, JDOM builds upon the Java collections. Like DOM, JDOM keeps the entire XML tree structure in memory while processing it. Therefore, DOM and JDOM may take up much memory and CPU when they are used to process very large XML files.
SAX is an event-driven parsing API that is well suited to fastly parse very large XML files with little amount of memory. However, SAX does not allow you to modify or iterate through an XML tree structure.
JAXP (Java API for XML Processing) is the standard API provided by the JDK. A key point to note is that JAXP and JDOM are not parsers. In fact, to be able to parse XML, they need a parser (either a SAX or a DOM parser). JAXP allows you to specify the parser to use (via the command line or programmatically) and its API is parser-independent. By default, the parser used by JAXP is Xerces which implements DOM and SAX.
The default parser used by JDOM is the one specified by JAXP. If the parser specified by JAXP is not found, then Xerces is selected. There is no need to download and add Xerces to your classpath since a fork of it is bundled into the JDK.
Note that a JDOM document is very different from a DOM tree (They have different types. For example, you cannot assign a JDOM document to a DOM tree). You may have to work with JDOM in a situation where your JDOM-based application has to share XML documents with a DOM-based application. To address incompatibility issues, JDOM provides integration with DOM: you can convert from JDOM to DOM and vice versa.