Java Reference
In-Depth Information
Each type of event results in a different method in your program being called. There are, for example,
different events for registering the beginning and end of a document. You can also see that the start and end
of each element results in two further kinds of events, and another type of event occurs for each segment of
document data. Thus, this particular document involves five different methods in your program being called
— some of them more than once, of course, so there is one method for each type of event.
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 piecemeal 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 example — you can just save those as you receive them through the callback mechanism, and you can
discard the rest. As a consequence, SAX is a particularly fast and memory-efficient way of selectively pro-
cessing 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 JDK:
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 in-
formation about a DTD, or to obtain information about comments and CDATA sections in a doc-
ument.
In addition to these, the javax.xml.parsers package provides factory classes that you use to gain access
to a parser.
In Java terms there are several interfaces involved. The XMLReader interface specifies the methods that
the SAX parser calls 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.
Search WWH ::




Custom Search