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

XMLReaderValidator.java - Validating XML with XSD using Xerces-J

This section provides a tutorial example on how to download and install Xerces-J, and how to write a Java program to use the XMLReader interface implemented in Xerces-J to validate an XML against it linked XSD schema file.

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. http://www.herongyang.com/
 */
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());
      }
   }
}

Here is the linked XSD file, dictionary.xsd:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="dictionary" type="dictionaryType"/>
 <xsd:complexType name="dictionaryType">
  <xsd:sequence>
   <xsd:element name="word" type="wordType" maxOccurs="unbounded"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="wordType">
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string"/>
   <xsd:element name="definition" type="definitionType" 
    maxOccurs="unbounded"/>
   <xsd:element name="update" type="updateType" minOccurs="0"/>
  </xsd:sequence>
  <xsd:attribute name="acronym" type="xsd:boolean" use="optional"/>
  <xsd:attribute name="symbol" type="xsd:boolean" use="optional"/>
 </xsd:complexType>
 <xsd:complexType name="definitionType" mixed="true">
  <xsd:attribute name="reference" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="updateType">
  <xsd:attribute name="date">
   <xsd:simpleType>
    <xsd:restriction base="xsd:string">
     <xsd:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}"/>
    </xsd:restriction>
   </xsd:simpleType>
  </xsd:attribute>
 </xsd:complexType>
</xsd:schema>

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

Validating an XML Document against the Specified XML Schema

XMLReaderValidator.java - Validating XML with XSD using Xerces-J

 XMLReaderValidator.java - Validation Error Messages

 Using SAXParserFactory to Load XML Parsers

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2013
XMLReaderValidator.java - Validating XML with XSD using Xerces-J