XML Schema (XSD) Validation using XMLReader

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

After installing Xerces2 2.11.0 (XML Schema 1.1) (Beta) version, now I am ready to write a simple Java program to use "org.apache.xerces.parsers.SAXParser" class provided in Xerces2 to validate any XML files against the specified XSD files:

/* XMLReaderValidator.java
 - Copyright (c) 2013, HerongYang.com, 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());
      }
   }
}

First, let's try to compile and run XMLReaderValidator.java with JDK 1.7 without using Xerces2 library:

c:\Progra~1\Java\jdk1.7.0_07\bin\javac XMLReaderValidator.java

c:\Progra~1\Java\jdk1.7.0_07\bin\java XMLReaderValidator 
   dictionary_invalid_xsd.xml

org.xml.sax.SAXException: 
   SAX2 driver class org.apache.xerces.parsers.SAXParser not found
   java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

The error tells us that the org.apache.xerces.parsers.SAXParser class is not included in JDK 1.7.

Now try to run XMLReaderValidator.java with Xerces2 2.11.0 regular version:

c:\Progra~1\Java\jdk1.7.0_07\bin\java 
   -cp ".;c:\local\xerces-2_11_0\xercesImpl.jar" 
   XMLReaderValidator dictionary_invalid_xsd.xml

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'.

Error:
   Public ID: null
   System ID: file:///C:/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, 'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/herong/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}' for type
      '#AnonType_dateupdateType'.
      
Error:
   Public ID: null
   System ID: file:///C:/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, '#AnonType_dateupdateType'.
      
Error:
   Public ID: null
   System ID: file:///C:/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:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-datatype-valid.1.2.1: 'no' is not a valid value for 
      'boolean'.
      
Error:
   Public ID: null
   System ID: file:///C:/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, 'boolean'.

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

Last update: 2013.

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 API

XML Schema (XSD) Validation using XMLReader

 Running XMLReaderValidator with Xerces2 2.11.0 Beta

 Running XMLReaderValidator on XSD 1.1 Schema

 XML Schema (XSD) Validation using SAXParser

 SAXParser for XSD Validation in JDK 1.7

 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

 References

 PDF Printing Version