XSD Schema File Loader - XsdSchemaLoader.java

This section describes a tutorial example on how to create an XSD schema file loader using JAXP Schema and SchemaFactory classes.

As we can see from the standard steps described in the previous tutorial, the first thing we need to do is to create SchemaFactory instance. Then use it to create a Schema instance by parsing the schema file.

Here is simple tutorial example code called XsdSchemaLoader.java that parses a schema file and create a Schema instance:

/* XsdSchemaLoader.java
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
 */
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import java.io.File;
class XsdSchemaLoader {
  public static void main(String[] a) {
    if (a.length<1) {
      System.out.println("Usage:");
      System.out.println("java XsdSchemaLoader 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);
      System.out.println();
      System.out.println("Schema Language: "+language);
      System.out.println("Factory Class: "
        + factory.getClass().getName());

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

    } catch (Exception e) {
      // catching all exceptions
      System.out.println();
      System.out.println(e.toString());
    }
    return schema;
  }
}

As the first test, let's try to load the following simple XSD schema file, first_html.xsd:

<?xml version = "1.0" encoding = "utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="html" type="htmlType"/>
 <xs:complexType name="htmlType">
  <xs:sequence>
   <xs:element name="body" type="xs:string"/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

Here is what you will get as output in JDK 13, which has not been changed since JDK 1.6:

herong> java XsdSchemaLoader.java first_html.xsd

Schema Language: http://www.w3.org/2001/XMLSchema
Factory Class: com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory

Schema File: first_html.xsd
Schema Class: com.sun.org.apache.xerces.internal.jaxp.validation.SimpleXMLSchema

The output shows that:

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

 Standard Steps to Validate XML Documents Against a Schema

XSD Schema File Loader - XsdSchemaLoader.java

 XSD Schema File Loading Errors

 XSD Schema XML DOM Validator - XsdSchemaDomValidator.java

 XSD Schema XML DOM Validation Errors

 XSD Schema XML DOM Validator with Error Handler

 XSD Schema XML SAX Validator - XsdSchemaSaxValidator.java

 XSD Schema XML SAX Validation Errors

 XSD Schema XML SAX Validator with Error Handler

 XSD Schema XML Validator - Final Version

 XSD 1.1 Not Supported in JDK 13

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java APIs

 XML Schema Language - Basics

 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

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB