XML technologies / XML / XML in a Nutshell / The Simple API for XML (SAX)

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

SAX, the Simple API for XML, is an event-based XML parsing API implemented by many parsers, including Xerces, Crimson, MSXML, the Oracle XML Parser for Java, and Ælfred. Originally Java-only, SAX primarily targets Java parsers, so this section focuses on Java. Ports exist for most object-oriented languages, including C++, Python, Perl and Eiffel. Porting from Java is usually straightforward.

Unlike most XML APIs, SAX uses an event-driven push model rather than tree-based pulling. As the XML parser reads, it feeds the application information immediately for every start tag, end tag, character datum or processing instruction. The document appears progressively, beginning to end. Keep interesting sections for later or process them immediately; no need to await the document’s end. Crucially, no complete in-memory copy is required, making SAX ideal for documents too large for RAM.

Note: We cover only SAX2. In 2004 all major SAX parsers support SAX2. Its main addition over SAX1 is namespaces, changing almost every method and class name or signature. Old SAX1 APIs remain but are deprecated and should be avoided.

SAX mainly consists of Java interfaces in the package org.xml.sax. One is XMLReader, abstracting the actual parser. Its methods parse documents and configure options such as validation. Create an instance of XMLReader using the class XMLReaderFactory from the package org.xml.sax.helpers. Its static method createXMLReader( ) creates a parser-specific implementation of XMLReader, choosing the class through the system property org.xml.sax.driver:

try {
  XMLReader parser = XMLReaderFactory.createXMLReader(  );
  // Das Dokument parsen ...
}
catch (SAXException e) {
  // Die Erzeugung des XMLReader schlug fehl
}

Wrapping XMLReaderFactory.createXMLReader( ) in a try-catch-block is typical, to catch a possible SAXException. The SAXException is a generic superclass for any XML reading or parsing problem. Here it might mean the system property org.xml.sax.driver is unset or its named Java class is missing from the classpath.

Warning! Avoid the classes in JAXP, SAXParserFactory and SAXParser. Sun created them to fill a SAX1 gap. In SAX2 they are unnecessary and dangerous, ignoring by default namespaces. Use XMLReaderFactory and XMLReader in SAX2 applications instead.

Alternatively, choose a particular parser by passing its class name as a string to createXMLReader( ), as with Xerces here:

try {
  XMLReader parser = XMLReaderFactory.createXMLReader(
   "org.apache.xerces.parsers.SAXParser");
  // Das Dokument parsen ...
}
catch (SAXException ex) {
  // Erzeugung des XMLReader schlug fehl
}

Once created, the parser can read. Pass the document’s SYSTEM identifier to the method parse( ). It is almost always an absolute or relative URL string. This example reads the document at http://www.slashdot.org/slashdot.xml:

try {
  XMLReader parser = XMLReaderFactory.createXMLReader(  );
  parser.parse("http://www.slashdot.org/slashdot.xml");
}
catch (SAXParseException ex) {
  // Das Dokument ist nicht wohlgeformt.
}
catch (SAXException ex) {
  // Konnte die Implementierung von XMLReader nicht finden.
}
catch (IOException ex) {
  // Irgendein I/O-Problem hat das komplette Herunterladen des
  // Dokuments vom Server verhindert.
}

The method parse( ) also throws a SAXParseException for malformed documents, an IOException on reading problems and a SAXException for other failures. Otherwise it returns nothing (void). To receive document information, first install a ContentHandler.

   

   

 

 

 

back to top