Validating XML Linked with XSD using XMLReader

This section provides a tutorial example on how to validate XML files that contain XSD location references with org.xml.sax.XMLReader class. It requires Xerces-J JAR files to be installed.

Another way to validate an XML file is to use the XMLReader class, if the XML file contains a reference to the XSD schema file.

But the XMLReader requires a SAX Parser implementation class. The most popular SAX Parser implementation is probably the "org.apache.xerces.parsers.SAXParser" class from the Xerces-J package. Here is my example program, XMLReaderValidator.java, which requires the Xerces-J package:

/* XMLReaderValidator.java
 * Copyright (c) 2014-2018 HerongYang.com. All Rights Reserved.
 */
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
class XMLReaderValidator {
   public static void main(String[] args) {
      String parserClass = "org.apache.xerces.parsers.SAXParser";
      String validationFeature
         = "http://xml.org/sax/features/validation";
      String schemaFeature
         = "http://apache.org/xml/features/validation/schema";
      try {
         String x = args[0];
         XMLReader r = XMLReaderFactory.createXMLReader(parserClass);
         r.setFeature(validationFeature,true);
         r.setFeature(schemaFeature,true);
         r.setErrorHandler(new MyErrorHandler());
         r.parse(x);
      } catch (SAXException e) {
         System.out.println(e.toString());
      } catch (IOException e) {
         System.out.println(e.toString());
      }
   }
   private static class MyErrorHandler extends DefaultHandler {
      public void warning(SAXParseException e) throws SAXException {
         System.out.println("Warning: ");
         printInfo(e);
      }
      public void error(SAXParseException e) throws SAXException {
         System.out.println("Error: ");
         printInfo(e);
      }
      public void fatalError(SAXParseException e) throws SAXException {
         System.out.println("Fatal error: ");
         printInfo(e);
      }
      private void printInfo(SAXParseException e) {
         System.out.println("   Public ID: "+e.getPublicId());
         System.out.println("   System ID: "+e.getSystemId());
         System.out.println("   Line number: "+e.getLineNumber());
         System.out.println("   Column number: "+e.getColumnNumber());
         System.out.println("   Message: "+e.getMessage());
      }
   }
}

Note that setFeature() method is called twice:

Compile and run it with JDK on dictionary_invalid_xsd.xml

herong> java XMLReaderValidator dictionary_invalid_xsd.xml

org.xml.sax.SAXException:
   SAX2 driver class org.apache.xerces.parsers.SAXParser not found
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

Okay. To resolve the error, we needed to go to http://xerces.apache.org/xerces2-j/ and download the binary distribution package, Xerces-J-bin.2.11.0.zip, of Xerces2 Java 2.11.0.

Unzip the package into \local\xerces-2_11_0 directory and make sure that xercesImpl.jar is in that directory.

Now run the XMLReaderValidator.java program again with xercesImpt.jar:

herong> java -cp .;\local\xerces-2_11_0\xercesImpl.jar \
   XMLReaderValidator dictionary_invalid_xsd.xml

org.apache.xerces.parsers.SAXParser@19bb367

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid value for
      'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-attribute.3: The value 'yes' of attribute 'acronym' on
      element 'word' is not valid with respect to its type, 'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-pattern-valid: Value '23-Dec-2014' is not facet-valid
      with respect to pattern '\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}' for type
      '#AnonType_dateupdateType'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 11
   Column number: 31
   Message: cvc-attribute.3: The value '23-Dec-2014' of attribute
      'date' on element 'update' is not valid with respect to its
      type, '#AnonType_dateupdateType'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 20
   Column number: 33
   Message: cvc-complex-type.3.2.2: Attribute 'editor' is not allowed
      to appear in element 'update'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-datatype-valid.1.2.1: 'no' is not a valid value for
      'boolean'.

Error:
   Public ID: null
   System ID: file:///C:/herong/dictionary_invalid_xsd.xml
   Line number: 22
   Column number: 36
   Message: cvc-attribute.3: The value 'no' of attribute 'symbol' on
      element 'word' is not valid with respect to its type, 'boolean'.

My test program, XMLReaderValidator, works! The XMLReader API and the Xerces-J package is able to automatically load the XSD file and perform the XSD schema validation while parsing the XML document.

Note that org.xml.sax.helpers.XMLReaderFactory class has been deprecated since JDK 9. So stop using it.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

Validating XML Documents Against Specified XML Schemas

 XSD Schema Validator on XML DOM Object

 XSD Schema Validator on XML DOM Object - Errors

 XSD Schema Validator on XML SAX Object

 SAXParseException - 'xsi:noNamespaceSchemaLocation' Not Allowed

Validating XML Linked with XSD using XMLReader

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB