Main Page

Previous Next

Accessing Parsers

The javax.xml.parsers package defines four classes supporting the processing of XML documents:

SAXParserFactory

Enables you to create a configurable factory object that you can use to create a SAXParser object encapsulating a SAX-based parser.

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 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.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor