XML and Java: SAX: Simple API for XML (Parsers)
December 9, 1998
URL: http://www.megginson.com/SAX/
Quickstart:
http://www.megginson.com/SAX/quickstart.html
Java Roadmap:
http://www.megginson.com/SAX/roadmap.html
Javadoc Documentation:
http://www.megginson.com/SAX/javadoc/packages.html
SAX stands for "Simple API for XML"; it is an extremely
popular (almost ubiquitous) event-based interface for any XML parser.
SAX is somewhat of a grass-roots effort, spearheaded by David
Megginson, of the
xml-dev mailing list.
As you can tell from the rest of this page, SAX is the basis
for quite a few Java APIs. Megginson sees SAX addressing a need
much like JDBC fills for SQL. The idea behind SAX is to provide
an interface by which any Java application can access any XML
parser, provided the parser has a SAX driver. Virtually every
major XML parser either supports the SAX interface directly
or indirectly via third-party drivers. SAX comes with two
sample drivers (Lark and MSXML).
SAX uses an event-driven model: the parser identifies an element,
resulting in that element's event handler being invoked. It is
optimized for applet/browser use and can plug-in to any particular
parser. We next present two lists taken from
Parsers and Applications Supporting SAX,
which contains links to all of the parsers and applications. Most of
these are discussed later in this WDVL article.
SAX Parsers (only those written in Java are listed):
- IBM's XML for Java
- James Clark's XP
- DataChannel's DXP
- Microstar's AElfred
- Silfide's SXP
- Sun's XML Library
There is also third party support for others. There are many
others SAX parsers and applications in Python.
SAX Applications (only those written in Java are listed):
- James Clark's XMLTest
- Peter Murray-Rust's JUMBO
- Michael Kay's SAXON
- Megginson Technologies' XAF
- Docuverse's DOM SDK
- JXML's Coins
- W3C's SiRPAC
- John Cowan's SAX ParserFilters and DOMParser
- WebEasy's Weasel
- Koala Project's KOML (Koala Object Markup Language)
- Koala Project's XSL Engine
The SAX 1.0 release consists of two packages,
org.xml.sax
and
org.xml.sax.helpers,
which the
roadmap
divides into five groups of classes:
- interfaces implemented by SAX-conformant parsers (e.g., a SAXDriver);
- interfaces implemented by applications (e.g., DocumentHandler and ErrorHandler);
- standard SAX classes (e.g., InputSource and SAXException);
- optional Java-specific helper (convenience) classes (e.g., ParserFactory);
and
- Java demonstration classes in the nul package.
The
core SAX packages
can be downloaded separately from the
sample SAXDrivers.
The code examples below from David Megginson's
Quick Start
page illustrate how easy it is to use SAX. The first file,
MyHandler.java,
subclasses HandlerBase (itself a subclass of
DocumentHandler)
to define a class that will handle events. In this basic example,
only the startElement
and EndElement methods are defined, but there are
others to choose from and certainly more interesting things can
be done with the AttributeList that is sent to the
startElement method. Interested readers should
read Megginson's javadoc for
AttributeList.
import org.xml.sax.HandlerBase;
import org.xml.sax.AttributeList;
public class MyHandler extends HandlerBase {
public void startElement (String name, AttributeList atts)
{
System.out.println("Start element: " + name);
}
public void endElement (String name)
{
System.out.println("End element: " + name);
}
}
The main program, SAXApp.java, defines the
parserClass (Java classpath for the parser of
choice -- Microstar's AElred in this example), calls
ParserFactory.makeParser to instantiate a parser,
instantiates MyHandler, and associates the handler
as the DocumentHandler. The beauty of SAX is that
to switch to a different parser, the only line in both files that
must change is the one that defines the parserClass. (You might want
to switch to a different parser if, for example, a faster parser
becomes available.) Therefore, by using SAX, your application
code remains the same across different SAX-conformant parsers.
import org.xml.sax.Parser;
import org.xml.sax.DocumentHandler;
import org.xml.sax.helpers.ParserFactory;
public class SAXApp {
static final String parserClass = "com.microstar.xml.SAXDriver";
public static void main (String args[])
throws Exception
{
Parser parser = ParserFactory.makeParser(parserClass);
DocumentHandler handler = new MyHandler();
parser.setDocumentHandler(handler);
// Parse all files given on the command line.
for (int i = 0; i < args.length; i++) {
parser.parse(args[i]);
}
}
}
XML and Java: The Perfect Pair: Part 2: Java APIs for XML
XML and Java: The Perfect Pair: Part 2: Java APIs for XML
XML and Java: Java Project X (formerly XML Library from JavaSoft)
|