XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
JAXP, DOM and SAX APIs in Xerces2 JARs
This section describes Xerces2 APIs, JAXP, DOM and SAX, which are also offered in the JDK package. If your application is using these APIs, it may behave differently depending on from where these APIs are provided.
In the previous chapter, we have learned how to use two sample programs, jaxp.SourceValidator and jaxp.TypeInfoWriter, provided in the Xerces2 package to perform XSD (XML Schema) 1.1 validations.
In this chapter, we will learn how to write our own Java programs to perform XSD (XML Schema) validation using Xerces2 Java APIs.
If you read Xerces2 Java API documentation, you will see it offers 3 sets of standard APIs in the xml-apis.jar file:
Note that all these 3 APIs are also offered in the JDK package. If your application is using these APIs, it may behave differently depending on from where these APIs are provided.
And don't forget that the JDK package actually uses an older version of Xerces2 as default implementations for both DOM and SAX parsers. So it could be very confusing.
Let's use our JAXP test program, XsdSchemaLoader.java, to see the difference of with and without Xerces2 JAR files:
/* XsdSchemaLoader.java - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. */ import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import javax.xml.XMLConstants; import java.io.File; class XsdSchemaLoader { public static void main(String[] a) { if (a.length<1) { System.out.println("Usage:"); System.out.println("java XsdSchemaLoader schema_file_name"); } else { String name = a[0]; Schema schema = loadSchema(name); } } public static Schema loadSchema(String name) { Schema schema = null; try { // getting the default implementation of XML Schema factory String language = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory factory = SchemaFactory.newInstance(language); System.out.println(); System.out.println("Schema Language: "+language); System.out.println("Factory Class: " + factory.getClass().getName()); // parsing the schema file schema = factory.newSchema(new File(name)); System.out.println(); System.out.println("Schema File: "+name); System.out.println("Schema Class: " + schema.getClass().getName()); } catch (Exception e) { // catching all exceptions System.out.println(); System.out.println(e.toString()); } return schema; } }
Run XsdSchemaLoader.java in JDK 13 without Xerces2. JAXP API and implementations classes from JDK are used:
herong> java XsdSchemaLoader.java hello.xsd Schema Language: http://www.w3.org/2001/XMLSchema Factory Class: com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory Schema File: hello.xsd Schema Class: com.sun.org.apache.xerces.internal.jaxp.validation.SimpleXMLSchema
Run XsdSchemaLoader.java in JDK 13 with Xerces2 2.12.1 JAR files. JAXP API and implementations classes from Xerces2 are used:
herong> type java_xerces.bat java -cp .;.\xerces-2_12_1-xml-schema-1.1\xml-apis.jar; ^^^.\xerces-2_12_1-xml-schema-1.1\xercesImpl.jar; ^^^.\xerces-2_12_1-xml-schema-1.1\serializer.jar; ^^^.\xerces-2_12_1-xml-schema-1.1\resolver.jar.jar; ^^^.\xerces-2_12_1-xml-schema-1.1\org.eclipse.wst.xml.xpath2.processor_1.2.0.jar; ^^^.\xerces-2_12_1-xml-schema-1.1\xercesSamples.jar; %1 %2 %3 %4 %5 %6 herong> java_xerces XsdSchemaLoader.java hello.xsd Schema Language: http://www.w3.org/2001/XMLSchema Factory Class: org.apache.xerces.jaxp.validation.XMLSchemaFactory Schema File: hello.xsd Schema Class: org.apache.xerces.jaxp.validation.SimpleXMLSchema
The output shows that different versions of implementation classes, XMLSchemaFactory and SimpleXMLSchema, are used when calling JAXP API with and without Xerces2 JAR files.
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