Skip to main content

Tip: Converting from SAX

Using SAX to communicate with apps that need DOM and JDOM inputs

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:  Brett McLaughlin explains how to use SAX to communicate with applications that require DOM and JDOM inputs. It's a useful technique: With the flurry of XML APIs available, developers now have to be able to easily move from one to another, and then on to another. The sample code provides a concrete example of converting from SAX to JDOM.

View more content in this series

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

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.

From SAX to DOM

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!


From SAX to JDOM

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!


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

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