This section provides a tutorial example on calling SchemaFactory.newInstance() with 'http://www.w3.org/XML/XMLSchema/v1.1' for XSD 1.1 validation.
After reading documentations from Apache Xerces Website, I see why my XMLReaderValidator.java program
failed to validate XSD 1.1 schema documents. The "org.apache.xerces.parsers.SAXParser" class
is using a default configuration that assumes XSD 1.0, not XSD 1.1.
In order to support XSD 1.1, we need to find a way to change the configuration setting to use:
"http://www.w3.org/XML/XMLSchema/v1.1" as the schema language.
Unfortunately, there seems to be no way to modify this setting in the way how the
"org.apache.xerces.parsers.SAXParser" class is used in my XMLReaderValidator.java program:
So I need to go back and modify my other schema validation program to use the
"javax.xml.validation.SchemaFactory" for XSD 1.1 validation:
/* Xsd11SchemaValidator.java
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
*/
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class Xsd11SchemaValidator {
private static int errorCount = 0;
public static void main(String[] a) {
if (a.length<2) {
System.out.println("Usage:");
System.out.println("java Xsd11SchemaValidator schema_file_name "
+ "xml_file_name");
} else {
String schemaName = a[0];
String xmlName = a[1];
Schema schema = loadSchema(schemaName);
validateXml(schema, xmlName);
}
}
public static void validateXml(Schema schema, String xmlName) {
try {
// creating a Validator instance
Validator validator = schema.newValidator();
// setting my own error handler
validator.setErrorHandler(new MyErrorHandler());
// preparing the XML file as a SAX source
SAXSource source = new SAXSource(
new InputSource(new java.io.FileInputStream(xmlName)));
// validating the SAX source against the schema
validator.validate(source);
System.out.println();
if (errorCount>0) {
System.out.println("Failed with errors: "+errorCount);
} else {
System.out.println("Passed.");
}
} catch (Exception e) {
// catching all validation exceptions
System.out.println();
System.out.println(e.toString());
}
}
public static Schema loadSchema(String name) {
Schema schema = null;
try {
// String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
String language = "http://www.w3.org/XML/XMLSchema/v1.1";
SchemaFactory factory = SchemaFactory.newInstance(language);
schema = factory.newSchema(new File(name));
} catch (Exception e) {
System.out.println(e.toString());
}
return schema;
}
private static class MyErrorHandler implements ErrorHandler {
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();
}
}
}
Notice that how the "SchemaFactory.newInstance" method is used
to create a new schema object with language="http://www.w3.org/XML/XMLSchema/v1.1" parameter.