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). 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() 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, this code fragment build an org.jdom.Document object from it:
DOMBuilder builder = new DOMBuilder(); org.jdom.Document jdomDocument = builder.build(domDocument); // 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 it was built from. In the other direction, 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 instance, 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.
|Copyright 2001, 2002 Elliotte Rusty Haroldemail@example.com||Last Modified April 18, 2002|
|Up To Cafe con Leche|