Java Reference
In-Depth Information
SAXParser : Defines an object that wraps a SAX-based parser.
DocumentBuilderFactory : Enables you to create a configurable factory object that you can use
to create a DocumentBuilder object encapsulating a DOM-based parser.
DocumentBuilder : Defines an object that wraps a DOM-based parser.
All four classes are abstract. This is because JAXP is designed to allow different parsers and their factory
classes to be plugged in. Both DOM and SAX parsers are developed independently of the Java JDK, so it is
important to be able to integrate new parsers as they come along. The Xerces parser that is currently distrib-
uted with the JDK is controlled and developed by the Apache Project, and it provides a very comprehensive
range of capabilities. However, you may want to take advantage of the features provided by other parsers
from other organizations, and JAXP allows for that.
These abstract classes act as wrappers for the specific factory and parser objects that you need to use for
a particular parser and insulate your code from a particular parser implementation. An instance of a factory
object that can create an instance of a parser is created at runtime, so your program can use a different parser
without changing or even recompiling your code. Now that you have a rough idea of the general principles,
let's get down to specifics and practicalities, starting with SAX.
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 returns a reference to an object of a class type that is a concrete implementation of SAXParserFact-
ory . This is 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 creates. For example, the SAXParserFactory object has methods for determin-
ing whether the parser that it attempts to create is namespace-aware or validates the XML as it is parsed. The
isNamespaceAware() method returns true if the parser it creates is namespace-aware and returns false
otherwise. The isValidating() method returns true if the parser it creates validates the XML during pars-
ing and returns 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 pars-
ers that are not namespace-aware. A parser that is namespace-aware can recognize the structure of names
in a namespace — with a colon separating the namespace prefix from the name. A namespace-aware parser
can report the URI and local name separately for each element and attribute. A parser that is not namespace-
aware reports only an element or attribute name as a single name, even when it contains a colon. In other
words, a parser that is not namespace-aware treats a colon as just another character that is part of a name.
Similarly, calling the setValidating() method with an argument value of true causes the factory object
to produce parsers that can validate the XML. A validating parser can verify that the document body has a
DTD or a schema, and that the document content is consistent with the DTD or schema identified within the
document.
Search WWH ::




Custom Search