Checking Schema Documents -

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,

 - Copyright (c) 2002-2013 All Rights Reserved.
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
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 technology which is included in JDK since JDK 1.6. Compilation and test output is listed below. To learn more about this program, read the JAXP chapters in this book.

herong> java -version
java version "13" 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)

herong> javac

herong> java XsdSchemaChecker
java XsdSchemaChecker schema_file_name

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 APIs

XML Schema Language - Basics

 Schema and Schema XML Representation

Checking Schema Documents -

 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


 Archived Tutorials


 Full Version in PDF/EPUB