Java Reference
In-Depth Information
Properties of a Parser
You can set the properties for a parser by calling the setProperty() method for the SAXParser
object once you have created it. The first argument is the name of the property as type String and the
second argument is the value for the property. The property value can be of any class type as the
parameter type is Object but it is usually of type String . The setProperty() method will throw a
SAXNotRecognizedException if the property name is not recognized or
SAXNotSupportedException if the property name is recognized but not supported. Both of these
exception classes are defined in the org.xml.sax package.
You can also retrieve the values for some properties during parsing to obtain additional information
about the most recent parsing event. You use the parser's getProperty() method in this case. The
argument to the method is the name of the property and the method returns a reference to the
property's value.
As with features, there is no defined set of parser properties so you need to consult the parser
documentation for information on these. At the time of writing there are four standard properties for a
SAX parser, but since these involve the more advanced features of SAX parser operation, they are
beyond the scope of this topic.
Parsing Documents with SAX
To parse a document using the XMLParser object you simply call its parse() method. You have to
supply two arguments to the parse() method. The first identifies the XML document and the second
is a reference of type DefaultHandler to a handler object that you will have created to process the
contents of the document. The DefaultHandler object must contain a specific set of public methods
that the XMLParser object expects to be able to call for each event, where each type of event
corresponds to a particular syntactic element it finds in the document.
The DefaultHandler class that is defined in the org.xml.sax.helpers package already contains
do-nothing definitions of all the callback methods that the XMLParser object expects to be able to call.
Thus all you have to do is to define a class that extends the DefaultHandler class, and then override
the methods in the DefaultHandler class for the events that you are interested in. But let's not gallop
too far ahead. We need to look into the versions of the parse() method that we have available before
we get into handling parsing events.
The XMLParser class defines ten overloaded versions of the parse() method but we are only
interested in five of them. The other five use a now deprecated handler type, HandlerBase , that was
applicable to SAX1, so we shall ignore those and just look at the versions that relate to SAX2. All
versions of the method have a return type of void , and the five varieties of the parse() method that
we will consider are:
Search WWH ::




Custom Search