You've decided that SAX is the best thing since sliced bread. It's fast, slick, and makes you sound cool: you're a SAX programmer. Not many others are these days, however, because DOM and JDOM make it easier for you to handle some tasks. (Although there are other times when SAX, simply put, rocks!) But let's say you've got to get your XML data from the SAX format you are working with into the format that those other guys writing applications need -- again, most likely either DOM or JDOM. In this tip, I show you how to move from SAX into DOM and also into JDOM. Well, that's not technically true: I'll actually explain how SAX is not quite the same as DOM or JDOM, and how you can use SAX in conjunction with these other APIs.
First, let's assume that you want to convert from SAX to DOM. Right off the bat, this description of the example project isn't really accurate. You see, unless you do it manually, SAX doesn't store an XML document as any sort of in-memory representation. Therefore, you will never find yourself on the receiving end of an XML document "in SAX format." That said, it's not uncommon to want to build a DOM tree using SAX. SAX is fast, so what better means is there to create a DOM tree? You'll find, in fact, that most parsers actually use SAX in some form to create DOM trees.
Take Apache Xerces as an example. The DOMParser and SAXParser
classes actually are extensions of a single class, org.apache.xerces.framework.XMLParser.
And the parse() method is defined in that superclass, rather than
in each subclass, which means that parsing occurs in the same fashion in
each subclass. In other words, when you use the parse() method
for either SAX or DOM, the same code is being called. The primary difference
is that the DOMParser class exposes an additional method, getDocument(),
which returns a DOM tree after parsing is complete. So, while you can't
explicitly "convert" from SAX to DOM, it may be happening every time you
use your parser without you even knowing it!
Converting from SAX to JDOM is walking the same sort of blurry line
as converting from SAX to DOM. It doesn't really make sense to say, "I
converted my document from SAX to DOM." Like DOM, however, JDOM can use
SAX to build a JDOM Document, and that turns out to be the fastest
means of document creation (at least currently). To perform this creation
using SAX, you would want to use the JDOM SAXBuilder class. An
example of this is shown in Listing 1.
// Java imports
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
// JDOM imports
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class SAXtoJDOM {
// XML file to read
File file;
public SAXtoJDOM(File file) {
this.file = file;
}
public Document convert() throws JDOMException, IOException {
// Create new SAXBuilder, using default parser
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new FileInputStream(file));
return doc;
}
public static void main(String[] args) {
try {
File file = new File(args[0]);
SAXtoJDOM tester = new SAXtoJDOM(file);
Document doc = tester.convert();
// Output the document to System.out
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
There you have it. Moving from SAX to DOM or SAX to JDOM is somewhat of a misnomer. But using SAX to build DOM and JDOM trees is simple, as both often use SAX "under the covers." With this tip in-hand, you're now a little wiser, and you can really show off to your friends!
-
Visit the birthplace of SAX, Dave Megginson's SAX page. There you'll find release downloads, history, a list of known bugs, and more.
-
Find out more about JDOM, including a project overview, downloads, and links to mailing lists.
-
Explore the complete world of DOM, starting with the W3C page on DOM.
-
Catch up with other recent tips in the developerWorks XML zone:
- Using XSLT as a shortcut to Web page tables of contents
- Documenting style sheets with RDF
- Using lookup tables in XSLT
- Moving DOM nodes (without triggering the
Wrong-documentexception) - Using JDOM with XSLT
- Using SAX and SAX helper classes to achieve vendor independence
- For a complete list of XML tips to date, check out the tips summary page.
- IBM trial software: Build your next development project with trial software available for download directly from developerWorks.
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.
