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

Standard Steps to Validate XML Documents Against a Schema

This section describes the standard steps of loading an XML schema file and use it to validate XML documents.

If you want to use the JAXP included in JDK 1.6 to validate XML documents against XML schema files, you need to follow the standard steps described below:

1. Create a SchemaFactory instance by selecting the default implementation of the XML Schema language. For example, sfactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI). W3C_XML_SCHEMA_NS_URI is pre-defined constant of "http://www.w3.org/2001/XMLSchema", the fully-qualified URI for W3C XML Schema language.

2. The SchemaFactory instance can then be used to read a schema file, parse it, and create a Schema instance. for example, schema = sfactory.newSchema(new File(name)). The newSchema() method will throw exceptions, if there are any errors while parsing the specified schema file.

3. When the Schema instance is loaded with the XSD file, it can be used to create a Validator instance. The Validator instance is now ready to be used to validate any XML files presented as a DOMSource, SAXSource or other XML sources. For example, validator = schema.newValidator().

4a. One way to feed the XML file to the Validator instance is to present it as a DOMSource object. This can be done in 4 steps:

  • Create a DocumentBuilderFactory instance with the default implementation class. For example, bfactory = DocumentBuilderFactory.newInstance().
  • The DocumentBuilderFactory instance can then be used to create a DocumentBuilder instance with the default implementation class. For example, builder = bfactory.newDocumentBuilder().
  • The DocumentBuilder instance can then be used to read the XML file, parse it, and create a Document instance. For example, document = builder.parse(new File(name)).
  • The last step is to simply convert the Document instance into a DOMSource object using the constructor. For example, source = new DOMSource(document).

4b. Another way to feed the XML file to the Validator instance is to present it as a SAXSource object. This can be done in 3 steps:

  • First open the XML file as a FileInputStream instance. For example, stream = new java.io.FileInputStream(name).
  • Then convert the FileInputStream instance as an InputSource instance. For example, input = new InputSource(stream).
  • The last step is to simply convert the InputSource instance into a SAXSource object using the constructor. For example, source = new SAXSource(input).

5. With the XSD file parsed and represented as a Validator instance, and the XML file represented as a Source object, validating the XML file can be done by calling the validate() method. For example, validator.validate(source).

Several tutorial examples will be provided in this chapter to show you how to perform different steps mentioned above.

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

Standard Steps to Validate XML Documents Against a Schema - Updated in 2014, by Dr. Herong Yang