SAX Support in the IDE
SAX, the simple API for XML, is a serial access protocol for XML. SAX is an
event-driven API that you can use in your Java programs by registering your
document handler with a SAX parser. After registration, the parser invokes your
callback methods whenever it encounters a new XML tag, an error, or other special
condition.
You typically use this protocol with servlets and network-oriented programs.
SAX is the fastest and least memory-intensive mechanism currently available for
dealing with XML documents. If you are writing an application that displays an
XML document and possibly modifies it, you might prefer to use the DOM mechanism.
Before you can generate a SAX document handler that can read your XML files,
you must have a DTD defined for the files you want to handle. The IDE uses the
DTD file to generate the following files:
- Generated Parser. A parser which implements org.xml.sax.DocumentHandler
(or org.xml.sax.ContentHandler for SAX 2.0) and uses its methods
to parse the XML files. The parser translates and normalizes the output from
the SAX parser and calls the appropriate handler implementation methods or
parslet implementation methods.
- Handler Interface. The interface containing the methods that
are called by the generated parser as it parses the XML document. You can
specify which elements the IDE generates handler methods for. The IDE generates
a handle_elementName method for content elements and a pair
of start_elementName and end_elementName methods
for container elements.
- Handler Implementation. A sample implementation of the handler
interface with skeleton code generated for each of the interface's methods.
You should add your handling logic here.
- Parslet Interface. (Optional) An interface that defines methods
for converting the string output from the generated parser. The methods convert
the output into qualified data types. The generated parser then passes the
converted output to the handler.
- Parslet Implementation. (Optional) A sample implementation of
the parslet interface. The implementation contains skeleton code generated
for each of the interface's methods. You should add the converting logic here.
This approach to generating SAX document handlers has the following benefits:
- It cleanly decouples the generated parser's dispatching logic from application
logic.
- It cleanly decouples data type parsing logic from application handling.
The data type parsing libraries can be reused.
- It simplifies code updates when you update your DTD. You can regenerate
the parsers and handlers without losing your earlier customizations.
For a tutorial on serial access with SAX, see http://java.sun.com/xml/docs/tutorial/sax/index.html.
Legal Notices