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

XML Schema (XSD) Validation using XMLReader

This section describes a tutorial example on how to the Xerces2 XMLReader class to validate an XML document assigned with an XSD file.

Note that tutorial examples given in this section were taken in 2002 using JDK 1.4 and Xerces-J 2.3. But the example Java program can still run using JDK 1.6 and Xerces2 2.9.1

Unfortunately, I couldn't find any XML parsers provided in J2SDK 1.4.1_02 that can validate XML structure against XSD rules. So I downloaded one of the most popular XML parsers in the public domain, xerces-j 2.3.0, at: http://xml.apache.org/dist/xerces-j.

Once I downloaded Xerces-J-bin.2.3.0.zip, I unzipped it into \local\xerces-2_3_0 directory. Make sure that xercesImpt.jar is in that directory.

Now I am ready to write a simple Java program to use "org.apache.xerces.parsers.SAXParser" class to validate any XML files against the specified XSD files:

/**
 * XMLReaderValidator.java
 * Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
 */
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
class XMLReaderValidator {
   public static void main(String[] args) {
      String parserClass = "org.apache.xerces.parsers.SAXParser";
      String validationFeature 
         = "http://xml.org/sax/features/validation";
      String schemaFeature 
         = "http://apache.org/xml/features/validation/schema";
      try {
      	 String x = args[0];
         XMLReader r = XMLReaderFactory.createXMLReader(parserClass);
         r.setFeature(validationFeature,true);
         r.setFeature(schemaFeature,true);
         r.setErrorHandler(new MyErrorHandler());
         r.parse(x);
      } 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());
      }
   }
}

Now run the XMLReaderValidator program validate dictrionary_invalid_xsd.xml:

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

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

Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-attribute.3: The value 'yes' of attribute 'acronym' on
   element 'word' is not valid with respect to its type.

Error:
   Public ID: null
   System ID: file:///D:/herong/src/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-pattern-valid: Value '23-Dec-2003' is not facet-valid 
   with respect to pattern '\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}'.

Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-attribute.3: The value '23-Dec-2003' of attribute 
   'date' on element 'update' is not valid with respect to its type.

Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 20
   Column number: 33
   Message: cvc-complex-type.3.2.2: Attribute 'editor' is not allowed
   to appear in element 'update'.

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

Error:
   Public ID: null
   System ID: file:///D:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-attribute.3: The value 'no' of attribute 'symbol' on
   element 'word' is not valid with respect to its type.

This is perfect. It tells you where the error is, and why it's an error.

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

XML Parser API - Xerces2 Java Parser

 Installing Xerces2 Java Parser

 Testing Examples of XSD File and XML File

XML Schema (XSD) Validation using XMLReader

 XML Schema (XSD) Validation using SAXParser

 XML Schema Language - Basics

 XML Schema Built-in Datatypes

 Complex Element Declaration

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
XML Schema (XSD) Validation using XMLReader