∟XSD Schema XML SAX Validator - XsdSchemaSaxValidator.java
This section describes a tutorial example on how to create a Validator instance from an XSD Schema instance to validate an XML file using the JAXP SAX interface.
After creating an XSD Schema instance from the XSD schema file,
we are ready to create a Validator instance from the schema instance.
This Validator instance can then be used to validate an XML file represented
with the DOM interface or the SAX interface.
We have tried the DOM interface in previous sections. Now we want to try
the SAX interface.
Here is a tutorial example program called XsdSchemaSaxValidator.java
that validates an XML file against an XSD file using the SAX interface:
/*
* XsdSchemaSaxValidator.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.*;
class XsdSchemaSaxValidator {
public static void main(String[] a) {
if (a.length<2) {
System.out.println("Usage:");
System.out.println("java XsdSchemaValidator 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();
System.out.println();
System.out.println("Validator Class: "
+ validator.getClass().getName());
// 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();
System.out.println("Validation 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;
SchemaFactory factory = SchemaFactory.newInstance(language);
schema = factory.newSchema(new File(name));
} catch (Exception e) {
System.out.println(e.toString());
}
return schema;
}
}
As the first test, let's try to validate the following simple XML file, first_html.xml,
against the simple XSD schema file, first_html.xsd:
>type first_html.xsd
<?xml version = "1.0" encoding = "utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="html" type="htmlType"/>
<xs:complexType name="htmlType">
<xs:sequence>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
>type first_html.xml
<?xml version = "1.0" encoding = "utf-8"?>
<html>
<body>My first HTML document in XML format.</body>
</html>
>javac XsdSchemaSaxValidator.java
>java XsdSchemaSaxValidator first_html.xsd first_html.xml
Validator Class: com.sun.org.apache.xerces.internal.jaxp
.validation.ValidatorImpl
Validation passed.
The output shows that:
The default implementation class of the Validator class is also from the Apache Xerces project.
The validation passed successfully. No exceptions were raised.