Skip to main content

Tip: Converting from JDOM

JDOM doesn't map exactly to DOM and SAX, but you can easily output to both

Brett McLaughlin (brett@newInstance.com), Enhydra strategist, Lutris Technologies
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.

Summary:  Learning to work with JDOM? Well, no API is an island, and in this tip you'll learn how to convert with ease from JDOM to SAX and DOM for fluent programming of XML applications that use all three APIs. The code samples provide examples of converting from JDOM to SAX and from JDOM to DOM.

View more content in this series

Date:  01 Apr 2001
Level:  Introductory
Activity:  1884 views
Comments:  

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.

From JDOM to SAX

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);
}    


From JDOM to DOM

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!


Resources

About the author

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.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=11992
ArticleTitle=Tip: Converting from JDOM
publish-date=04012001
author1-email=brett@newInstance.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers