O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

22.10. Using SAX

To process an XML document with SAX, you first have to establish contact with the parser that you want to use. The first step toward this is to create a SAXParserFactory object like this:

SAXParserFactory spf = SAXParserFactory.newInstance();

The SAXParserFactory class is defined in the javax.xml.parsers package along with the SAXParser class that encapsulates a parser. The SAXParserFactory class is abstract but the static newInstance() method will return a reference to an object of a class type that is a concrete implementation of SAXParserFactory. This will be the factory object for creating an object encapsulating a SAX parser.

Before you create a parser object, you can condition the capabilities of the parser object that the SAXParserFactory object will create. For example, the SAXParserFactory object has methods for determining whether the parser that it will attempt to create will be namespace aware or will validate the XML as it is parsed:

isNamespaceAware()Returns true if the parser to be created is namespace aware, and false otherwise
isValidating()Returns true if the parser to be created will validate the XML during parsing, and false otherwise

You can set the factory object to produce namespace aware parsers by calling its setNamespaceAware() method with an argument value of true. An argument value of false sets the factory object to produce parsers that are not namespace aware. A parser that is namespace aware can recognize the structure of names in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required