This section describes a tutorial example on how to use the Xerces2 XMLReader class to validate an XML document assigned with an XSD file.
After installing Xerces2 2.11.0 (XML Schema 1.1) (Beta) version,
now I am ready to write a simple Java program to use "org.apache.xerces.parsers.SAXParser"
class provided in Xerces2 to validate any XML files against the specified XSD files:
First, let's try to compile and run XMLReaderValidator.java with JDK 1.7 without using Xerces2 library:
c:\Progra~1\Java\jdk1.7.0_07\bin\javac XMLReaderValidator.java
c:\Progra~1\Java\jdk1.7.0_07\bin\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
The error tells us that the org.apache.xerces.parsers.SAXParser class
is not included in JDK 1.7.
Now try to run XMLReaderValidator.java with Xerces2 2.11.0 regular version:
c:\Progra~1\Java\jdk1.7.0_07\bin\java
-cp ".;c:\local\xerces-2_11_0\xercesImpl.jar"
XMLReaderValidator dictionary_invalid_xsd.xml
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-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.