XML XSD Schema Validation with Xerces

This section provides a tutorial example on how to write a XML schema validation program, XMLReaderValidator.java, with the Xerces parser.

If you are still using JDK 1.4.1, XMLReaderValidator2.java will not work, because JDK 1.4.1 has no built-in class to support XML validation against XSD rules. So you have to downloaded one of the most popular XML parsers in the public domain, Xerces-J at: http://xml.apache.org/dist/xerces-j.

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

Now I am ready to use "org.apache.xerces.parsers.SAXParser" from the Xerces-J parser to validate any XML files against the specified XSD files:

/* XMLReaderValidator.java
 - Copyright (c) 2014, 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.setErrorHandler(new MyErrorHandler());
      } catch (SAXException e) {
      } catch (IOException e) {
   private static class MyErrorHandler extends DefaultHandler {
      public void warning(SAXParseException e) throws SAXException {
         System.out.println("Warning: "); 
      public void error(SAXParseException e) throws SAXException {
         System.out.println("Error: "); 
      public void fatalError(SAXParseException e) throws SAXException {
         System.out.println("Fattal error: "); 
      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 I used the XMLReaderFactory.createXMLReader() method to create an XMLReader with the specified Xerces parser class.

Now run the XMLReaderValidator program:

>java -cp .;\local\xerces-2_11_0\xercesImpl.jar XMLReaderValidator

You will get the same validation error report as running XMLReaderValidator2.java.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

XSD (XML Schema Definition) - XML Validation

 XML XSD Schema Validation

 XML XSD Schema Validation Test Result

XML XSD Schema Validation with Xerces

 XSD Validation Failed with SAXParserFactory

 XSD Schema Validator on XML SAX Object

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version