XML Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Using SAX Implementation Provided in JDK 1.4

This section provides a tutorial example on how to use the SAX interface included in JDK 1.4, which actually uses SAX implementation provided by the org.apache.crimson package.

I have a simple program here, SAXClassChecker.java, to show what are the implementation classes provided JDK 1.4 for the SAX interfaces:

/**
 * SAXClassChecker.java
 * Copyright (c) 2002 by Dr. Herong Yang. http://www.herongyang.com/
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
class SAXClassChecker {
   public static void main(String[] args) {
      try {
         File x = new File(args[0]);
         SAXParserFactory f = SAXParserFactory.newInstance();
         System.out.println(f.toString());
         SAXParser p = f.newSAXParser();
         System.out.println(p.toString());
         DefaultHandler h = new DefaultHandler();
         System.out.println(h.toString());
         p.parse(x,h);
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString()); 
      } catch (SAXException e) {
         System.out.println(e.toString()); 
      } catch (IOException e) {
         System.out.println(e.toString()); 
      }
   }
}

If you run this sample program, you will get:

C:\herong\xml>java SAXClassChecker hello.xml
org.apache.crimson.jaxp.SAXParserFactoryImpl@1c78e57
org.apache.crimson.jaxp.SAXParserImpl@1e63e3d
org.xml.sax.helpers.DefaultHandler@1be2d65

Note that:

  • javax.xml.parsers.SAXParserFactory.newInstance() method is used to create a new fatory instance using the factory implementation class, org.apache.crimson.jaxp.SAXParserFactoryImpl.
  • javax.xml.parsers.SAXParserFactory.newSAXParser() method is used to create a new parser instance using the parser implementation class, org.apache.crimson.jaxp.SAXParserImpl.
  • javax.xml.parsers.SAXParser.parse() method is used to invoke the parsing process. It requires two parameters, a File object representing the XML file, and an parsing event handler object.
  • org.xml.sax.helpers.DefaultHandler is dummy handler, which handles all the events fired by the parser, but does nothing. This is why we are getting nothing from the XML file when running this program.

Notes and sample codes in this section are based on JDK 1.4.1_01.

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

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2013
Using SAX Implementation Provided in JDK 1.4