Java Reference
In-Depth Information
SAX originated as an XML parsing API for Java. However, SAX is not exclusive
to Java. Microsoft also supports SAX for its .NET framework (see
ht-
AftertakingyouonatouroftheSAXAPI,thissectionprovidesasimpledemonstra-
tion ofthis API to help youbecome familiar with its event-based parsing paradigm. It
then shows you how to create a custom entity resolver.
Exploring the SAX API
SAX exists in two major versions. Java implements SAX 1 through the
javax.xml.parsers
package'sabstract
SAXParser
and
SAXParserFactory
classes, and implements SAX 2 through the
org.xml.sax
package's
XMLReader
interface and through the
org.xml.sax.helpers
package's
XMLReaderFact-
ory
class. The
org.xml.sax
,
org.xml.sax.ext
, and
org.xml.sax.helpers
packagesprovidevarioustypesthataugmentbothJavaim-
plementations.
Note
IexploreonlytheSAX2implementationbecauseSAX2makesavailablead-
ditionalinfosetitemsaboutanXMLdocument(suchascommentsandCDATAsection
notifications).
Classes that implement the
XMLReader
interface describe SAX 2-based parsers.
Instances of these classes are obtained by calling the
XMLReaderFactory
class's
createXMLReader()
methods. For example, the following example invokes this
class's
static XMLReader createXMLReader()
methodtocreate andreturn
an
XMLReader
instance:
XMLReader xmlr = XMLReaderFactory.createXMLReader();
Thismethodcallreturnsaninstanceofan
XMLReader
-implementingclassandas-
signs its reference to
xmlr
.
Note
Behindthescenes,
createXMLReader()
attemptstocreatean
XMLRead-
er
instancefromsystemdefaults,accordingtoalookupprocedurethatfirstexamines
the
org.xml.sax.driver
system property to see if it has a value. If so, this
property's value is used as the name of the class that implements
XMLReader
, and
an attempt is made to instantiate this class and return the instance. An instance of