Java Reference
In-Depth Information
XML elements, comments, and other items. In using a SAX parser, you provide an event
handler that accepts the events the SAX parser sends and builds the resulting object from
those events. Your event handler implements the interface defined by the org.xml.sax.
helpers.DefaultHandler class, overriding its methods to handle individual SAX events.
Some of the events you can choose to handle include the following:
startDocument : Signals the beginning of the XML document.
endDocument : Signals the end of the XML document.
startElement : Signals the start of an element and is given the element's name,
namespace, and attributes. Attributes are passed using an instance of an
Attributes class that provides a collection of all of the attributes of an element,
making it easy for you to find the value of a specific attribute.
endElement : Signals the end of an element.
characters : Takes character data from within an element.
Returning to the WeatherWidget example, Listing 13-8 shows the
LocationParserHandler class—a subclass of DefaultHandler that parses an XML document
representing a Location object with its associated forecast.
Listing 13-8. The LocationParserHandler SAX Handler
package com.apress.rischpater.weatherwidget;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class LocationParserHandler extends DefaultHandler {
private Location location;
private boolean setForecast;
private boolean hasForecast;
private boolean setTemp;
private String precipitation, precipType, precipProb;
private String windSpeed, windDirection;
private StringBuffer buffer;
public LocationParserHandler(Location l) {
location = l;
}
 
Search WWH ::




Custom Search