XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
XML Schema (XSD) Validation using XMLReader
This section describes a tutorial example on how to use the Xerces2 XMLReader class to validate an XML document assigned with an XSD file.
With the understanding of Xerces2 JAR files override JAXP, DOM and SAX APIs with its own features, now I am ready to write a simple Java program using SAX API and a given Xerces2 SAX parser, "org.apache.xerces.parsers.SAXParser", to validate any XML files against the specified XSD files:
/* XMLReaderValidator.java - Copyright (c) 2002-2013 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("Fattal 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()); } } }
First, let's try to compile and run XMLReaderValidator.java with JDK and without using Xerces2:
herong> java XMLReaderValidator.java dictionary_invalid_xsd.xml Note: XMLReaderValidator.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. org.xml.sax.SAXException: SAX2 driver class org.apache.xerces.parsers.SAXParser not found java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
The error tells us that the org.apache.xerces.parsers.SAXParser class is not included in JDK.
Now try to run XMLReaderValidator.java with Xerces2 JAR files:
herong> java_xerces XMLReaderValidator.java dictionary_invalid_xsd.xml Note: XMLReaderValidator.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 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: 8 Column number: 10 Message: cvc-complex-type.2.4.a: Invalid content was found starting with element 'names'. One of '{name}' is expected. 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-2003' 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-2003' 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'.
This is perfect. It tells you where the error is, and why it's an error.
Note that XMLReaderFactory class has been deprecated since JDK 9. I need to find a way to replace it.
Table of Contents
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
JAXP, DOM and SAX APIs in Xerces2 JARs
►XML Schema (XSD) Validation using XMLReader
Running XMLReaderValidator on XSD 1.1 Schema
XML Schema (XSD) Validation using SAXParser
SAXParser for XSD Validation Fixed
SAXParser for XSD 1.1 Validation
Xsd11SchemaValidator.java for XSD 1.1 Validation
Xsd11SchemaValidator.java XSD 1.1 Test Result
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
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