CSci 4608 Principles of Web Programming: XML schema validation

Below are a sample XML file and a sample XML Schema, followed by a program that validates the file against the schema using a SAX ("Sample API for XML") parser Xerces-J.

The additional class ShowValidationErrors defines an error handler that signals errors. If you want parsing to stop at the first validation error and throw an exception, uncomment the line throw exception in the error method. Currently the parsing continues when errors are encountered so that all validation errors are displayed. Well-formedness errors (i.e. when the source file is not a valid XML) stop the program.

The XML file shiporder.xml (contains a couple of errors to demostrate the validation):. Note a reference to shiporder.xsd as an attribute of the root element.

<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923"
 <orderperson>John Smith</orderperson>
  <name>Ola Nordmann</name>
  <address>Langgt 23</address>
  <city>4000 Stavanger</city>
  <title>Empire Burlesque</title>
  <note>Special Edition</note>
  <title>Hide your heart</title>

The XML Schema file shiporder.xsd:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="">

<xs:element name="shiporder">
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
   <xs:element name="item" maxOccurs="unbounded">
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
  <xs:attribute name="orderid" type="xs:string" use="required"/>


Java validation program (see compilation/running instructions below):

import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.SAXParser;

public class CheckSchema {

    public static void main(String [] args) throws IOException, SAXException {

        // Set the filenames here:
        String xmlSource = "shiporder.xml";
        String xsdSchema = "shiporder.xsd";

        // strings to set up properties
        String W3C_XML_SCHEMA =
        String JAXP_SCHEMA_SOURCE =

        // the parser variable
        XMLReader parser;

        // create the XML parser
        try {
            // get a new parser from the factory
            parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

            // XML Schema is used as the schema language
            parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            // Set up the schema file

            // both lines are needed to make the program
            // validate against the schema.
            // Otherwise it just validates well-formedness
            parser.setFeature("", true);

            // default contents handler
            parser.setContentHandler(new DefaultHandler());
            // set customized error handler (needed to display validation errors)
            parser.setErrorHandler(new ShowValidationErrors());

        } catch  (SAXException e) {
            // print out the error message
            System.out.println("Cannot create a parser");
            // rethrow the exception
            throw e;

        try {
            FileInputStream theStream = new FileInputStream(xmlSource);
            parser.parse(new InputSource(theStream));
        } catch (IOException e) {
            System.out.println("Cannot open a file");
            throw e;
        } catch (SAXException e) {
            System.out.println("The document is invalid");
            throw e;


The error handling class:

import org.xml.sax.*;

public class ShowValidationErrors implements ErrorHandler {
    public void warning(SAXParseException exception) {
        // Ignore warnings
        System.out.println("WARNING: " + exception.getMessage());

    public void error(SAXParseException exception) {
        System.out.println("VALIDATION ERROR: " + exception.getMessage());
        // throw exception;


    public void fatalError(SAXParseException exception)
        throws SAXException {
        System.out.println("WELL-FORMEDNESS ERROR: " + exception.getMessage());
        // A well-formedness error
        throw exception;



How to compile and run this code:

javac -classpath .:/home/elenam/4608_playpen/xerces-2_5_0/xercesImpl.jar
java -classpath .:/home/elenam/4608_playpen/xerces-2_5_0/xercesImpl.jar CheckSchema

Note that the code uses the SAX parser defined in the Xerces-2_5_0 package which is currently availble in my home directory. The standard package in /usr/share/java doesn't have the correct classes. If you get an error message about SAXParser class not found, double-check your compilation/running commands.
CSci 2101 home page

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.