SAX Support in the IDE
The Simple API for XML (SAX) is a serial access protocol for XML. SAX is an
event-driven API that you can use in your Java programs by registering a document
handler with a SAX parser. After registration, the parser invokes your callback
methods whenever it encounters a new XML tag, error, or other special condition.
You typically use the SAX 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 and
possibly modifies an XML document, you might prefer to use the DOM mechanism.
Before you can generate a SAX document handler that can read your XML files,
you define a DTD for the files you want to handle. The IDE uses the DTD to generate
the following files:
- Generated Parser. The parser used to parse the XML files. If
you generate the document handler using SAX version 1.0, the generated parser
implements org.xml.sax.DocumentHandler. If you use SAX version 2.0,
the parser implements org.xml.sax.ContentHandler. 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
the generated parser calls when parsing the XML document. You can specify
the elements for which the IDE generates handler methods. 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 for each of the interface's methods. Add your
handling logic here.
- (Optional) Parslet Interface. 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.
- (Optional) Parslet Implementation. A sample implementation
of the parslet interface. The implementation contains skeleton code for each
of the interface's methods. Add converting logic here.
The IDE's 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