Gotcha!
Not to disappoint, I want to warn you of some common JDOM pitfalls. I hope this will save you a little time in your JDOM programming.
JDOM isn’t DOM
First and foremost, you should realize
that JDOM isn’t DOM. It doesn’t wrap DOM, and
doesn’t provide extensions to DOM. In other words, the two have
no technical relation to each other. Realizing this basic truth will
save you a lot of time and effort; there are many articles out there
today that talk about getting the DOM interfaces to use JDOM, or
avoiding JDOM because it hides some of DOM’s methods. These
statements confuse more people than almost anything else. You
don’t need to have the DOM interfaces, and DOM calls (like
appendChild( )
or createDocument( )
) simply won’t work on JDOM. Sorry, wrong API!
Null Return Values
Another
interesting facet of JDOM, and one that has raised some controversy,
is the return values from methods that retrieve element content. For
example, the various getChild( )
methods on the
Element
class may return a null
value. I mentioned this, and demonstrated it, in the
PropsToXML
example code. The gotcha occurs when
instead of checking if an element exists (as was the case in the
example code), you assume that an element already exists. This is
most common when some other application or component sends you XML,
and your code expects it to conform to a certain format (be it a DTD,
XML Schema, or simply an agreed-upon standard). For example, take a
look at the following code:
Document doc = otherComponent.getDocument( ...
Get Java and XML, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.