Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

17.4 -Critical Xerces Packages

You have seen a decent handful of new classes so far, in several different packages. Before going deeper, we want to try to wrap up what you have seen and what you are about to see with a summarized subset of the Xerces API.

Listed in Tables 17.3 through 17.10 are the critical packages, classes, interfaces, and exceptions of the Java Xerces2 parser (with descriptions extracted from the API documentation). Take a look at these names and read the table a few times—if you become familiar with these types, you will soon feel comfortable building applications with Java Xerces. (Note that this is not an exhaustive list, so see the official API reference for a full package, class, interface, and exception list.)

The java.xml.parsers package lets you construct and access the actual parsers and document builders.

TABLE 17.3 The java.xml.parsers Package

Class

Descriptions

DocumentBuilder

-Defines the API to obtain DOM Document instances from an XML document or to create a new DOM Document

DocumentBuilderFactory

-Enables applications to obtain a parser that produces DOM object trees from XML documents

SAXParser

-Defines the API that wraps an XMLReader implementation class

SAXParserFactory

-Enables applications to configure and obtain a SAX-based parser to parse XML documents

Exception

Description

ParserConfigurationException

Indicates a serious configuration error

Error

Description

FactoryConfigurationError

-Thrown when a problem with configuration of the parser factories exists


The org.w3c.dom package includes all the DOM Level 2 XML elements, which are listed in Table 17.4.

TABLE 17.4 The org.w3c.dom Package

Interface

Description

Attr

Represents an attribute in an Element object.

CDATASection

-Used to escape blocks of text containing characters that would otherwise be regarded as markup.

CharacterData

-Extends Node with a set of attributes and methods for accessing character data in the DOM.

Comment

-Inherits from CharacterData and represents the content of a comment, that is, all the characters between the starting '<!––' and ending '––>'.

Document

Represents the entire HTML or XML document.

DocumentFragment

-A lightweight or minimal Document object used to represent portions of an XML Document larger than a single node.

DocumentType

-Each document has a doctype attribute whose value is either null or a DocumentType object ("DocumentType" as in DTD).

DOMImplementation

-Provides methods for performing operations independent of a particular document object model instance.

Element

-The majority of objects (apart from text) in a document are Element nodes.

Entity

-Represents an entity, either parsed or unparsed, in a XML document.

EntityReference

-May be inserted into the structure model when an entity reference is in the source document or when the user wants to insert an entity reference.

NamedNodeMap

-Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can be accessed by name.

Node

-The primary datatype for the entire Document Object Model and each of its constituent elements.

NodeList

-Provides the abstraction of an ordered collection of nodes.

Notation

-Represents notation declared in the DTD.

ProcessingInstruction

-Used to place processor-specific information in the text of the document.

Text

-Inherits from CharacterData and represents the textual content (called "character data" in XML) of an Element or Attr.

DOMException

Encapsulates a DOM error.


Use the DOM helper classes in org.w3c.dom.traversal in Table 17.5 to perform advanced iteration down your XML trees.

The essence of XML processing with SAX is to create an object that implements special interfaces (which receive SAX messages) and handle only the XML elements that interest you. Table 17.6 lists the primary interfaces you can implement.

TABLE 17.5 The org.w3c.dom.traversal Package

Interface

Description

DocumentTraversal

-Contains methods that create Iterators to traverse a node and its children.

NodeFilter

-Filters are objects that know how to filter out nodes (very useful, because the DOM does not provide any filters. These are quite easy to write, as well).

NodeIterator

-Used for stepping through a set of nodes.

TreeWalker

-Used to navigate a document tree or subtree using the view of the document.


TABLE 17.6 The org.w3c.sax Package

Interface

Description

Attributes

-Interface for a list of XML attributes.

ContentHandler

-Receives notification of the logical content of a document.

DTDHandler

-Receives notification of basic DTD-related events.

EntityResolver

-Basic interface for resolving entities.

ErrorHandler

-Basic interface for SAX error handlers.

Locator

Interface for associating a SAX event with a document location.

XMLFilter

-Interface for an XML SAX filter—used to build an additional bridge between an XMLReader parser and an event-handling client.

XMLReader

Interface for reading an XML document by using callbacks.

InputSource

A single input source for an XML entity

SAXException

Encapsulates a general SAX error or warning.

SAXNotRecognizedException

Exception class for an unrecognized identifier.

SAXNotSupportedException

Exception class for an unsupported operation.

SAXParseException

Encapsulates an XML parse error or warning.


The classes in Table 17.7 help ease the burden of building Java XML SAX applications. Feel free to use them. Because they are part of the standard API, they are supported by all recent Java XML implementations (which means that if you move from Apache-Xerces to something else, your code hardly needs to change).

Use the classes and interfaces in org.apache.xml.serialize, shown in Table 17. 8, to construct or utilize output facilities.

TABLE 17.7 The org.w3c.sax.helpers Package

Class

Description

AttributesImpl

Default implementation of the Attributes interface.

DefaultHandler

Default base class for SAX2 event handlers.

LocatorImpl

Optional convenience implementation of Locator.

NamespaceSupport

Encapsulates namespace logic for use by SAX drivers.

ParserAdaptor

Adapts a SAX1 parser as a SAX2 XMLReader.

XMLFilterImpl

-Base class for deriving an XML filter (to sit between an XMLReader parser and an event-handling client).

XMLReaderAdaptor

Adapts a SAX2 XMLReader as a SAX1 parser.

XMLReaderFactory

Factory for creating an XML reader.


TABLE 17.8 The org.apache.xml.serialize Package

Interface

Description

DOMSerializer

-Interface for a DOM serializer implementation

Serializer

-Interface for a DOM serializer implementation, factory for DOM and SAX serializers, and static methods for serializing DOM documents

HTMLSerializer

-Implements an HTML/XHTML serializer supporting both DOM and SAX pretty serializing.

IndentPrinter

-Extends printer and adds support for indentation and line-wrapping.

OutputFormat

Specifies an output format to control the serializer.

Printer

-Responsible for sending text to the output stream or writer.

TextSerializer

-Implements a text serializer supporting both DOM and SAX serializing.

XHTMLSerializer

-Implements an XHTML serializer supporting both DOM and SAX pretty serializing.

XMLSerializer

-Implements an XML serializer supporting both DOM and SAX pretty serializing.


Additional useful classes are listed in Table 17.9.

If you need direct access to Xerces parsers, study the API for the org.apache.xerces.parsers package to access the classes listed in Table 17.10.

TABLE 17.9 The org.apache.xerces Package

Class

Description

DOMUtil

-Very useful helper functions—can get the first or last child elements; given a org.w3c.dom.Node, can get the name for a Node, a Node's siblings, and much more

EncodingMap

-A convenience class that handles conversions between Internet Assigned Numbers Authority (IANA) encoding names and Java encoding names, and vice versa

URI

-A class to represent a Uniform Resource Identifier (URI)—designed to handle the parsing of URIs and provide access to the various constituent components (scheme, host, port, userinfo, path, query string, and fragment)


TABLE 17.10 The org.apache.xerces.parsers Package

Class

Description

AbstractDOMParser

The base class of all DOM parsers

AbstractSAXParser

The base class of all SAX parsers

DOMASBuilderImpl

The Abstract Schema DOM builder class

DOMBuilderImpl

The Xerces DOM builder class

DOMParser

The main Xerces DOM parser class

SAXParser

The main Xerces SAX parser class


  • + Share This
  • 🔖 Save To Your Account