XML Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

SAX Content Handler Interface

This section describes the SAX 1.0 and 2.0 standards. SAX defines 4 interfaces under the org.xml.sax package name: DocumentHandler, ErrorHandler, DTDHandler and EntityResolver.

Unlike DOM, there is no formal specification for SAX. The original SAX Java implementation by the SAX Project is considered as the standard. There are 2 major versions released by the SAX Project:

  • SAX 1.0 - Released on May 12, 1998.
  • SAX 2.0 - Released on May 5, 2000. In SAX 2.0, the structure and concepts remained largely the same, although several key interfaces changed incompatibly.

The SAX API is defined in 4 interfaces under the org.xml.sax package:

  • org.xml.sax.DocumentHandler - This is the main interface of SAX. It defines event handler methods (callback methods) that an application should implement to handle events fired by the parser while it traverses the input XML files.
  • org.xml.sax.ErrorHandler - It defines error handler methods (callback methods) that an applications should implement to add special handling logics when the parse encountered parsing errors.
  • org.xml.sax.DTDHandler - If an application needs to work with notations and unparsed (binary) entities, it must implement this interface to receive notification of the NOTATION and ENTITY declarations.
  • org.xml.sax.EntityResolver - If an application needs to do redirection of URIs in documents (or other types of custom handling), it must provide an implementation of this interface.

The main interface of SAX, org.xml.sax.ContentHandler defines the following event handler methods to be implemented by applications:

  • startDocument(): Called when parsing reaches the beginning of the XML document.
  • endDocument(): Called when parsing reaches the end of the XML document.
  • startElement(): Called when parsing reaches the beginning of an XML element.
  • endElement(): Called when parsing reaches the end of an XML element.
  • characters(): Called when parsing reaches the end of an character section.
  • ignorableWhitespace(): Called when parsing reaches any ignorable white spaces between elements.

Of course, some of the event handlers will receive information parsed from the XML file as parameters. For example:

  • startElement() passes all the attributes as an org.xml.sax.Attributes object.
  • characters() passes all the characters of the parsed text as char[] object.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

SAX (Simple API for XML) Programming Interface

 What Is SAX (Simple API for XML)?

 Using SAX Implementation Provided in JDK 1.4

SAX Content Handler Interface

 SAXBrowser.java - SAX Interface Java Example

 SAX Parsing Pattern Example

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating an XML Document against the Specified XML Schema

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
SAX Content Handler Interface