Java Reference
In-Depth Information
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 SDK so it is
important to be able to integrate new parsers as they come along. As we will see, the Crimson parser that is
currently distributed with the SDK is controlled and developed by the Apache Project, but you may want to
take advantage of the new features provided by the Xerces parser from the same organization.
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 towards 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 a particular parser
object, normally the default parser that comes with the SDK. To use a different parser, you would need
to obtain a reference to a factory object for that parser. We will see how you can arrange to do this a
little later in this chapter.
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 of false sets the
factory object to produce parsers that are not namespace aware. A parser that is namespace aware
recognizes the structure of names in a namespace - with a colon separating the namespace prefix from
the name. A namespace aware parser will report the URI and local name separately for each element
and attribute. A parser that is not namespace aware will only report 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 will
treat a colon as just another character that is part of a name.
Search WWH ::




Custom Search