Validating XML Documents with Schema Locations - JAXP

This section describes a test on using JAXP standard parser interface to validate an XML document with schema location assigned inside the XML document.

Note that JAXP 1.4 does not support schema validation on the XML parser classes interfaces any more. The following testing program, XmlSchemaValidator.java, worked correctly on older JAXP releases:

/* XmlSchemaValidator.java
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
 */
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class XmlSchemaValidator {
  private static int errorCount = 0;
  public static void main(String[] a) {
    if (a.length<1) {
      System.out.println("Usage:");
      System.out.println("java XmlSchemaValidator xml_file_name");
    } else {
      String name = a[0];

      // parsing the XML file
      validateXml(name);
    }
  }
  public static void validateXml(String name) {
    try {
      String schemaFeature
         = "http://apache.org/xml/features/validation/schema";

      // getting the default implementation of SAX parser
      SAXParserFactory factory
         = SAXParserFactory.newInstance();

      // setting the factory for XML schema validation
      // this will not work any more in JAXP 1.4
      factory.setFeature(schemaFeature, true);

      SAXParser parser = factory.newSAXParser();
      System.out.println();
      System.out.println("Factory Class: "
        + factory.getClass().getName());
      System.out.println("Parser Class: "
        + parser.getClass().getName());

      System.out.println();
      System.out.println("Parsing and validating: "+name);
      parser.parse(new File(name), new MyHandler());
      System.out.println();
      if (errorCount>0) {
        System.out.println("Failed with errors: "+errorCount);
      } else {
        System.out.println("Passed.");
      }

    } catch (Exception e) {
      // catching all exceptions
      System.out.println();
      System.out.println(e.toString());
    }
  }
  private static class MyHandler extends DefaultHandler {
    public void warning(SAXParseException e) throws SAXException {
       System.out.println("Warning: ");
       printException(e);
    }
    public void error(SAXParseException e) throws SAXException {
       System.out.println("Error: ");
       printException(e);
    }
    public void fatalError(SAXParseException e) throws SAXException {
       System.out.println("Fattal error: ");
       printException(e);
    }
    private void printException(SAXParseException e) {
      errorCount++;
      System.out.println("   Line number: "+e.getLineNumber());
      System.out.println("   Column number: "+e.getColumnNumber());
      System.out.println("   Message: "+e.getMessage());
      System.out.println();
    }
  }
}

If you try the above program on hello_xsd_no_namespace.xml that has schema location assigned, you will still get a validation error:

herong> java XmlSchemaValidator hello_xsd_no_namespace.xml

Factory Class: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Parser Class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl

Parsing and validating: hello_xsd_no_namespace.xml
Error:
   Line number: 3
   Column number: 44
   Message: cvc-elt.1: Cannot find the declaration of element 'p'.

Failed with errors: 1

Conclusion: This test proves that JAXP 1.4 is no longer supports the schema validation feature identified as, "http://apache.org/xml/features/validation/schema", while parsing XML documents.

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

 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

 Assigning XML Schema Location in XML Documents

 Validating XML Documents with Schema Locations

Validating XML Documents with Schema Locations - JAXP

 Assigning XML Schema Location with Namespaces

 Testing XML Schema Location with Namespaces

 Testing XML Schema Location with Namespaces - JAXP

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB