If you are like me, you've jumped on the JDOM bandwagon. It's easy to work with (yes, as one of the JDOM authors I'm biased) and makes XML and Java fit well together. That said, I'd be a fool if I even suggested that JDOM could replace DOM and SAX and become the only Java and XML API. There are very different applications for DOM, SAX, and JDOM, and you'll almost certainly have to work with all three in your XML-based applications. As for JDOM, you'll need to convince co-workers and management, who may not be as cutting-edge as you, that you can use JDOM and still have your programs interact with the many other applications that aren't using JDOM. That means converting from JDOM to SAX and from JDOM to DOM. In this tip, I'll show you how to do both.
When working with SAX, everything is based on an incoming series of
events. The SAX programmer writes callback method implementations that
are the snippets of code executed upon these various event occurrences.
At the core of all SAX parsing is the SAX org.xml.sax.ContentHandler
interface, which defines the callbacks that are part of processing an XML
document. Additionally, the ErrorHandler, DTDHandler,
and EntityResolver interfaces are critical. Once you've set
up implementations of these interfaces, you can hand them off to the JDOM
org.jdom.output.SAXOutputter
class and sit back and relax. Listing 1 shows how this works and gives
you some ideas for your own programs.
Listing 1. Converting from JDOM to SAX
public convertToSAX(Document jdomDoc) throws JDOMException {
SAXOutputter outputter = new SAXOutputter(new MyContentHandlerImpl());
// Set the EntityResolver impl
outputter.setEntityResolver(new MyEntityResolverImpl());
// Set the DTDHandler impl
outputter.setEntityResolver(new MyDTDHandlerImpl());
// Set the ErrorHandler impl
outputter.setEntityResolver(new MyErrorHandlerImpl());
// Fire the SAX events
outputter.output(jdomDoc);
}
|
Converting from a JDOM structure to a DOM structure works in a similar
way. In fact, it's even easier than working with SAX, because DOM and JDOM
have somewhat analogous structures. There are no surprises here: Just as
you used SAXOutputter for outputting to SAX, you will need to
use the org.jdom.output.DOMOutputter class for outputting to DOM. Listing 2 shows the class in action.
Listing 2. Converting from JDOM to DOM
public org.w3c.dom.Document convertToDOM(org.jdom.Document jdomDoc)
throws JDOMException {
DOMOutputter outputter = new DOMOutputter();
return outputter.output(jdomDoc);
}
public org.w3c.dom.Element convertToDOM(org.jdom.Element jdomElement)
throws JDOMException {
DOMOutputter outputter = new DOMOutputter();
return outputter.output(jdomElement);
}
public org.w3c.dom.Attr convertToDOM(org.jdom.Document jdomAttribute)
throws JDOMException {
DOMOutputter outputter = new DOMOutputter();
return outputter.output(jdomAttribute);
}
|
Sure, there are some additional helpful methods in both the SAXOutputter
and DOMOutputter, but I've given you the basics. With this tip,
you are equipped to use JDOM, and use it with both applications that receive
SAX and DOM as input as well as applications that produce SAX and DOM,
if you read the other XML tips I've been writing (see Resources).
So go forth and prosper, interchanging XML in all its various formats!
-
Visit the birthplace of SAX at Dave
Megginson's site.
-
Find out more about JDOM at the JDOM
home page.
-
Look into the background of DOM at the W3C's
DOM page.
- Find more XML resources on the developerWorks XML zone. For a complete list of XML tips to date, check out the tips summary page.
Brett McLaughlin (brett@newInstance.com) works as Enhydra strategist at Lutris Technologies and specializes in distributed systems architecture. He is author of Java and XML (O'Reilly). He is involved in technologies such as Java servlets, Enterprise JavaBeans technology, XML, and business-to-business applications. Along with Jason Hunter, he founded the JDOM project, which provides a simple API for manipulating XML from Java applications. He is also an active developer on the Apache Cocoon project and the EJBoss EJB server as well as a co-founder of the Apache Turbine project.





