Java Reference
In-Depth Information
parser encounters the various element types of the XML document, it invokes the
corresponding event handler methods you've defined. Your methods take what-
ever actions are required to accomplish the desired task. In the SAX model, the
parser converts an XML document into a sequence of Java method calls. The
parser doesn't build a parse tree of any kind (although your methods can do this,
if you want). SAX parsing is typically quite efficient and is therefore your best
choice for most simple XML processing tasks.
The SAX API was created by David Megginson ( http://www.megginson.com/SAX/ ).
The Java implementation of the API is in the package org.xml.sax and its sub-
packages. SAX is a defacto standard but has not been standardized by any official
body. SAX Version 1 has been in use for some time; SAX 2 was finalized in May
2000. There are numerous changes between the SAX 1 and SAX 2 APIs. Many
Java-based XML parsers exist that conform to the SAX 1 or SAX 2 APIs.
With the SAX API, you can't completely abstract away the details of the XML
parser implementation you are using: at a minimum, your code must supply the
classname of the parser to be used. This is where JAXP comes in. JAXP is the Java
API for XML Parsing. It is an “optional package” defined by Sun that consists of the
javax.xml.parsers package. JAXP provides a thin layer on top of SAX (and on
top of DOM, as we'll see) and standardizes an API for obtaining and using SAX
(and DOM) parser objects. The JAXP package ships with default parser implemen-
tations but allows other parsers to be easily plugged in and configured using sys-
tem properties. At this writing, the current version of JAXP is 1.0.1; it supports SAX
1, but not SAX 2. By the time you read this, however, JAXP 1.1, which will include
support for SAX 2, may have become available.
Example 19-1 is a listing of ListServlets1.java , a program that uses JAXP and SAX to
parse a web application deployment descriptor and list the names of the servlets
configured by that file. If you haven't yet read Chapter 18, Servlets and JSP , you
should know that servlet-based web applications are configured using an XML file
named web.xml . This file contains <servlet> tags that define mappings between
servlet names and the Java classes that implement them. To help you understand
the task to be solved by the ListServlets1.java program, here is an excerpt from the
web.xml file developed in Chapter 18:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.davidflanagan.examples.servlet.Hello</servlet-class>
</servlet>
<servlet>
<servlet-name>counter</servlet-name>
<servlet-class>com.davidflanagan.examples.servlet.Counter</servlet-class>
<init-param>
<param-name>countfile</param-name> <!-- where to save state -->
<param-value>/tmp/counts.ser</param-value> <!-- adjust for your system-->
</init-param>
<init-param>
<param-name>saveInterval</param-name>
<!-- how often to save -->
<param-value>30000</param-value>
<!-- every 30 seconds -->
</init-param>
</servlet>
Search WWH ::




Custom Search