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

Using SAXParserFactory to Load XML Parsers

This section provides a tutorial example on how to use the static method SAXParserFactory.newInstance() to load an XML parser from a JAR file instead of the default XML parser from the JDK default JAR files.

Xerces-J package can also be loaded by the SAXParserFactory.newInstance() method. Here is my SAXValidator.java:

/**
 * SAXValidator.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.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
class SAXValidator {
   public static void main(String[] args) {
      String schemaFeature 
         = "http://apache.org/xml/features/validation/schema";
      try {
         File x = new File(args[0]);
         SAXParserFactory f = SAXParserFactory.newInstance();
         System.out.println(f.toString());
         f.setValidating(true);
         f.setFeature(schemaFeature,true);
         SAXParser p = f.newSAXParser();
         System.out.println(p.toString());
         DefaultHandler h = new MyErrorHandler();
         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()); 
      }
   }
   private static class MyErrorHandler extends DefaultHandler {
      public void warning(SAXParseException e) throws SAXException {
         System.out.println("Warning: "); 
         printInfo(e);
      }
      public void error(SAXParseException e) throws SAXException {
         System.out.println("Error: "); 
         printInfo(e);
      }
      public void fatalError(SAXParseException e) throws SAXException {
         System.out.println("Fattal error: "); 
         printInfo(e);
      }
      private void printInfo(SAXParseException e) {
         System.out.println("   Public ID: "+e.getPublicId());
         System.out.println("   System ID: "+e.getSystemId());
         System.out.println("   Line number: "+e.getLineNumber());
         System.out.println("   Column number: "+e.getColumnNumber());
         System.out.println("   Message: "+e.getMessage());
      }
   }
}

Note that the schema feature is set to the SAXParserFactory object.

Now if you run the program with:

java -cp . dictrionary_invalid_xsd.xml

You will get:

org.apache.crimson.jaxp.SAXParserFactoryImpl@1004901
org.xml.sax.SAXNotRecognizedException: Feature: http://apache.org/xml
/features/validation/schema

Note that the crimson package was loaded from the J2SDK library, and it doesn't support XSD validation.

But if you run the program with:

java -cp .;\local\xerces-2_3_0\xercesImpl.jar
   dictrionary_invalid_xsd.xml

you will get:

org.apache.xerces.jaxp.SAXParserFactoryImpl@f72617
org.apache.xerces.jaxp.SAXParserImpl@173831b
...

Note that the xerces package was loaded, and XSD validation was performed.

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

 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

 XMLReaderValidator.java - Validating XML with XSD using Xerces-J

 XMLReaderValidator.java - Validation Error Messages

Using SAXParserFactory to Load XML Parsers

 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 SAXParserFactory to Load XML Parsers