XML Schema (XSD) Validation using SAXParser

This section describes a tutorial example on how to the Xerces2 SAXParser class to validate an XML document assigned with an XSD file. The sample program SAXValidator.java works up to JDK 1.6. It is not working with JDK 1.7 and up.

Instead of using the XMLReader interface to parse XML documents with XSD validation, we can also use the SAXParser interface to do the same job. Here is my sample program SAXValidator.java:

/* SAXValidator.java
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
 */
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, not to the SAXParser object. Here are test results with different releases of JDK and Xerces:

Running SAXValidator.java with JDK 13 and without Xerces2 JAR files::

herong> java SAXValidator.java dictionary_invalid_xsd.xml
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl@6ab7a896
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@318ba8c8

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 6
   Column number: 49
   Message: cvc-elt.1.a: Cannot find the declaration of element 'dictionary'.

Running SAXValidator.java with JDK 13 and with Xerces2 2.12.1 JAR files::

herong> java_xerces SAXValidator.java dictionary_invalid_xsd.xml
org.apache.xerces.jaxp.SAXParserFactoryImpl@229d10bd
org.apache.xerces.jaxp.SAXParserImpl@4135c3b
Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 6
   Column number: 49
   Message: cvc-elt.1.a: Cannot find the declaration of element 'dictionary'.

As you can see, the latest implementations of SAXParser from JDK and Xerces2 behave the same say. They are not able to load the XSD document specified in the XML document as: xsi:noNamespaceSchemaLocation="dictionary.xsd" See next tutorial on how to resolve this issue.

Running SAXValidator.java with JDK 1.7 and without Xerces2 JAR files.: The XSD document specified in the XML document as: xsi:noNamespaceSchemaLocation="dictionary.xsd" was not loaded.

herong> javac XMLReaderValidator.java

herong> java SAXValidator dictionary_invalid_xsd.xml

com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl@e86da0
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@17c2891
Error:
   Public ID: null
   System ID: file:/C:/herong/dictionary_invalid_xsd.xml
   Line number: 3
   Column number: 49
   Message: cvc-elt.1: Cannot find the declaration of element 'dictionary'.

Running SAXValidator.java with JDK 1.4 and without Xerces2 JAR files: Feature "http://apache.org/xml/features/validation/schema" was not supported in JDK. JDK 1.4 uses the "crimson" implementation of the SAX API.

herong> javac SAXValidator.java

herong> java -cp . SAXValidator dictionary_invalid_xsd.xml

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

Running SAXValidator.java with JDK 1.4 with Xerces-J 2.3 JAR files: The SAXParser implementation in Xerces-J 2.3 was able to support feature = "http://apache.org/xml/features/validation/schema" and load the XSD document specified in the XML document as: xsi:noNamespaceSchemaLocation="dictionary.xsd".

herong> java -cp ".;\local\xerces-2_3_0\xercesImpl.jar"
   SAXValidator dictionary_invalid_xsd.xml

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

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid value for
   'boolean'.

...

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

Using Xerces2 Java APIs

 JAXP, DOM and SAX APIs in Xerces2 JARs

 XML Schema (XSD) Validation using XMLReader

 Running XMLReaderValidator on XSD 1.1 Schema

XML Schema (XSD) Validation using SAXParser

 SAXParser for XSD Validation Fixed

 SAXParser for XSD 1.1 Validation

 Xsd11SchemaValidator.java for XSD 1.1 Validation

 Xsd11SchemaValidator.java XSD 1.1 Test Result

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB