XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Checking Schema Documents - XsdSchemaChecker.java

This section describes a tutorial example on how to use JAXP (Java API for XML Processing) to check (compile) XML Schema (XSD) documents.

Now we know that a schema document is an XML document that contains the XML representation of a schema or a part of a schema. Before we start to write any schema as an XML document, we need to find a tool to help us checking syntax errors.

Since we learned JAXP (Java API for XML Processing) in previous chapters in this book, we can use it to write a simple schema checker, XsdSchemaChecker.java:

 * XsdSchemaChecker.java
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import java.io.File;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class XsdSchemaChecker {
  private static int errorCount = 0;
  public static void main(String[] a) {
    if (a.length<1) {
      System.out.println("java XsdSchemaChecker schema_file_name");
    } else {
      String name = a[0];
      Schema schema = loadSchema(name);
  public static Schema loadSchema(String name) {
    Schema schema = null;
    try {
      // getting the default implementation of XML Schema factory
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(language);
      factory.setErrorHandler(new MyErrorHandler());

      // parsing the schema file      
      schema = factory.newSchema(new File(name));
      System.out.println("Schema File: "+name);
      System.out.println("Parser Class: "
        + schema.getClass().getName());

    } catch (Exception e) {
      // errors are already handled
    } finally {
      // checking error counts
      if (errorCount>0) {
        System.out.println("Failed with "+errorCount+" errors.");
      } else {
    return schema;
  private static class MyErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) {
       System.out.println("Warning: "); 
    public void error(SAXParseException e) {
       System.out.println("Error: "); 
    public void fatalError(SAXParseException e) {
       System.out.println("Fattal error: "); 
    private void printException(SAXParseException e) {
      System.out.println("   Line number: "+e.getLineNumber());
      System.out.println("   Column number: "+e.getColumnNumber());
      System.out.println("   Message: "+e.getMessage());

This program requires JAXP 1.4 which is included in JDK 1.6. Compilation and test output is listed below. To learn more about this program, read the JAXP chapters in this book.

>java -version
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode)

>javac XsdSchemaChecker.java

>java XsdSchemaChecker
java XsdSchemaChecker schema_file_name

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 Language - Basics

 Schema and Schema XML Representation

Checking Schema Documents - XsdSchemaChecker.java

 Creating Schema Documents - "schema" Element

 Declaring Root Elements - "element" Element

 Specifying Element Datatype - "type" Attribute

 Using XML Schema Built-in Datatypes

 Using XML Schema Built-in Datatypes Incorrectly

 Validating XML Documents against Schema Documents

 Deriving New Simple Datatypes - "simpleType" Element

 Defining Complex Datatypes - "complexType" Element

 Validation Error Examples on Complex Datatypes

 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



 PDF Printing Version

Checking Schema Documents - XsdSchemaChecker.java - Updated in 2014, by Dr. Herong Yang