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

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 section, 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) 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:

>javac XsdSchemaLoader.java

>java XsdSchemaLoader 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:

  • The default implementation class of the XML Schema language factory is from the Apache Xerces project.
  • The default implementation class of the schema presentation is also from the Apache Xerces project.

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

 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

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 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

 References

 PDF Printing Version

XSD Schema File Loader - XsdSchemaLoader.java - Updated in 2014, by Dr. Herong Yang