JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

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.

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" to validate any XML files against the specified XSD files:

/**
 * XMLReaderValidator.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
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());
      }
   }
}

Note that:

  • I used the XMLReaderFactory.createXMLReader() method to create an XMLReader with the Xerces parser class.
  • I also used XMLReader.setFeature() to turn on two features: "validation" and "validation schema".
  • Of course, I need to use XMLReader.setErrorHandler() to provide my own parser error handler.

See the next section to see test result of XMLReaderValidator.java.

Last update: 2006.

Sections in This Chapter

XML XSD Schema Validation with Xerces

XML XSD Schema Validation Test Result

Loading External XML Parsers with SAXParserFactory

Dr. Herong Yang, updated in 2008
XML XSD Schema Validation with Xerces