The SAXParser class is a wrapper around an org.xml.sax.XMLReader class and is used to parse XML documents using the SAX version 2 API. Obtain a SAXParser from a SAXParserFactory. Call setProperty( ) if desired to set a property on the underlying parser. (See for a description of standard SAX properties and their values. Finally, call one of the parse( ) methods to parse an XML document from a stream, file, URL, or org.xml.sax.InputSource. The SAX API is an event-driven one. A SAX parser does not build a document tree to describe an XML document like a DOM parser does. Instead, it describes the XML document to your application by invoking methods on an object the application provides. This is the purpose of the org.xml.sax.helpers.DefaultHandler object that is passed to the parse( ) method: you subclass this class to implement the methods you care about, and the parser will invoke those methods at appropriate times. For example, when the parser encounters an XML tag in a document, it parses the tag, and calls the startElement( ) method to tell you about it. And when it finds a run of plain text, it passes that text to the characters( ) method. In Java 5.0, the reset( ) method restores a SAXParser to its original state so that it can be reused.

Instead of using one of the parse( ) methods of this class, you can also call getXMLReader( ) to obtain the underlying XMLReader object and work with it directly to parse the desired document. ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.