Talking to DOM Programs

JDOM is not an acronym. It does not stand for "Java Document Object Model." JDOM is not directly compatible with DOM (which is an acronym for Document Object Model). That is to say, a JDOM Element is not a DOM Element . The JDOM Element class does not implement the DOM Element interface. JDOM's Element class has methods that the DOM Element interface does not have and vice versa. You cannot pass a JDOM Element to a method that expects a DOM Element or a DOM Element to a method that expects a JDOM Element . The same is true for the JDOM Document class and the DOM Document interface, the JDOM Attribute class and the DOM Attr interface, the JDOM ProcessingInstruction class and the DOM ProcessingInstruction interface, and so forth.

That being said, JDOM does allow you to convert JDOM documents to and from DOM documents. I don't recommend this for new projects (which should pick one API or the other and stick with it), but it is useful for integrating new JDOM programs with legacy DOM code and vice versa.

If you already have a DOM Document object, then the org.jdom.input.DOMBuilder class can use it to generate the JDOM equivalent. The syntax is straightforward. Use the no-args DOMBuilder() constructor to create a DOMBuilder object, and then pass the DOM Document object to its build() method. For example, assuming that the variable domDocument points to an object of type org.w3c.dom.Document , the following code fragment will build an org.jdom.Document object from it:

 DOMBuilder builder = new DOMBuilder();  org.jdom.Document jdomDocument =; // work with the JDOM document... 

The original DOM object is not changed in any way. Furthermore, changes to the JDOM Document do not affect the DOM Document from which it was built. Likewise, future changes to the DOM Document do not affect the JDOM Document .

Moving in the other direction, from JDOM to DOM, the org.jdom.output.DOMOutputter class produces DOM Document objects from JDOM Document objects. Since this isn't really serialization, but rather than just converting from one model to another, there aren't nearly as many options to set as with XMLOutputter . For example, you can't add extra white space or select the encoding. (You could always do that later with a DOM serializer of some kind, if necessary.) For example:

 DOMOutputter converter = new DOMOutputter();  org.w3c.dom.Document domDocument = converter.output(jdomDocument); // work with the DOM document... 

Once again, these documents are not connected after the initial creation of one from the other. Changes to one are not reflected in the other.

Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191 © 2008-2017.
If you may any questions please contact us: