∟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.
Just to show you that JAXP 1.4 does not support schema validation on the XML parser classes interfaces any more,
I wrote the following testing program, XmlSchemaValidator.java
/*
* XmlSchemaValidator.java
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
*/
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
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("Usage:");
System.out.println("java XmlSchemaValidator xml_file_name");
} else {
String name = a[0];
// parsing the XML file
validateXml(name);
}
}
public static void validateXml(String name) {
try {
String schemaFeature
= "http://apache.org/xml/features/validation/schema";
// 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();
System.out.println("Factory Class: "
+ factory.getClass().getName());
System.out.println("Parser Class: "
+ parser.getClass().getName());
System.out.println();
System.out.println("Parsing and validating: "+name);
parser.parse(new File(name), new MyHandler());
System.out.println();
if (errorCount>0) {
System.out.println("Failed with errors: "+errorCount);
} else {
System.out.println("Passed.");
}
} catch (Exception e) {
// catching all exceptions
System.out.println();
System.out.println(e.toString());
}
}
private static class MyHandler extends DefaultHandler {
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();
}
}
}
If you try this on hello_xsd_no_namespace.xml that has schema location assigned,
you will still get a validation error:
>javac XmlSchemaValidator.java
>java XmlSchemaValidator hello_xsd_no_namespace.xml
Factory Class: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Parser Class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
Parsing and validating: hello_xsd_no_namespace.xml
Error:
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, "http://apache.org/xml/features/validation/schema", while parsing XML documents.