Java Reference
In-Depth Information
Because of the way SAX works, your application inevitably receives the document a piece at a time,
with no representation of the whole document. This means that if you need to have the whole document
available to your program with its elements and content properly structured you have to assemble it
yourself from the information supplied to your callback methods.
Of course, it also means that you don't have to keep the entire document in memory if you don't need
it, so if you are just looking for particular information from a document, all <phonenumber> elements
for instance, you can just save those as you receive them through the callback mechanism, and discard
the rest. As a consequence, SAX is a particularly fast and memory efficient way of selectively processing
the contents of an XML document.
First of all, SAX itself is not an XML document parser; it is a public domain definition of an interface to
an XML parser, where the parser is an external program. The public domain part of the SAX API is in
three packages that are shipped as part of the SDK:
org.xml.sax - this defines the Java interfaces specifying the SAX API and the
InputSource class that encapsulates a source of an XML document to be parsed.
org.xml.sax.helpers - this defines a number of helper classes for interfacing to a SAX parser.
org.xml.sax.ext - this defines interfaces representing optional extensions to SAX2 to
obtain information about a DTD, or to obtain information about comments and CDATA
sections in a document.
In addition to these, the javax.xml.parsers package provides factory classes that you use to gain
access to a parser and the javax.xml.transform package defines interfaces and classes for XSLT
1.0 processing of an XML document.
In Java terms there are several interfaces involved. The XMLReader interface that is defined in the
org.xml.sax package specifies the methods that the SAX parser will call as it recognizes elements,
attributes, and other components of an XML document. You must provide a class that implements these
methods and responds to the method calls in the way that you want.
DOM Processing
DOM works quite differently to SAX. When an XML document is parsed, the whole document tree is
assembled in memory and returned to your application as an object of type Document that
encapsulates it, as illustrated below.
Search WWH ::




Custom Search