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

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) 2007 by Dr. Herong Yang. 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 implemenation class of the XML Schema language factory is from the Apache Xerces project.
  • The default implemenation class of the schema presentation is also from the Apache Xerces project.

Sections in This Chapter

Standard Steps to Validate XML Docuements 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

Dr. Herong Yang, updated in 2007
XSD Schema File Loader - XsdSchemaLoader.java