Java 2EE and XML Development

Скачать в pdf «Java 2EE and XML Development»


The other types of event handlers defined in SAX exist to deal with more peripheral tasks in XML parsing. The EntityResolver interface enables the mapping of references to external sources such as databases or URLs. The ErrorHandler interface is implemented to handle special processing of SAXEx-ceptions. Finally, the DTDHandler interface is used to capture information about document validation as specified in the document’s DTD.


SAX also provides a convenience class called the org.xml.sax.help-ers.DefaultHandler, which implements all of the event handler interfaces. By extending the DefaultHandler class, your component has access to all of the available SAX events.


Now that we understand how SAX works, it is time to put JAXP to work with it. For an example, let us read in our earlier product catalog XML document using SAX events and JAXP. To keep our example short and relevant, we define a SAX event handler class that listens only for the endElement event. Each time a product element has been completely read by the SAX parser, we print a message indicating such. The code for this handler is shown in listing 2.6.

Скачать в pdf «Java 2EE and XML Development»