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,, worked correctly on older JAXP releases:

 - Copyright (c) 2002-2013 All Rights Reserved.
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
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("java XmlSchemaValidator xml_file_name");
    } else {
      String name = a[0];

      // parsing the XML file
  public static void validateXml(String name) {
    try {
      String schemaFeature
         = "";

      // 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("Factory Class: "
        + factory.getClass().getName());
      System.out.println("Parser Class: "
        + parser.getClass().getName());

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

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

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:
Parser Class:

Parsing and validating: hello_xsd_no_namespace.xml
   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, "", 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


 Archived Tutorials


 Full Version in PDF/EPUB