11 March 2010

Validating XML Documents against XML Schema using JAXP1.3 Validator

Here's an example on how to validate an XML document against an XML Schema using JAXP 1.3 Validator API.
Suppose you have the following XML Schema file :

(the full file path should be : org/daz/xml/validation/employee.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="employees" type="employeesType" />
<xsd:complexType name="employeesType">
<xsd:sequence>
<xsd:element name="employee" type="employeeType"
maxOccurs="unbounded" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="company" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:complexType name="employeeType">
<xsd:sequence>
<xsd:element ref="name" />
<xsd:element ref="role" maxOccurs="unbounded" minOccurs="1" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:element name="name">
<xsd:simpleType>
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:element>
<xsd:element name="role">
<xsd:simpleType>
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:element>
</xsd:schema>


And This XML File :

(the full file path should be : org/daz/xml/validation/employee.xml)
<?xml version="1.0" encoding="UTF-8"?>
<employees company="ArxICT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<employee id="MHWDY">
<name>Mohammed Hewedy</name>
<role>SW Engineer</role>
</employee>
<employee id="MBSRY">
<name>Mohammed Besary</name>
<role>QA Engineer</role>
<role>Business Analyst</role>
</employee>
<employee id="EDWD">
<name>Eslam Dawood</name>
<role>SW Engineer</role>
</employee>
</employees>


Here's the code that validate the XML Againest the Schema :

package org.daz.xml.validation;

import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class EmployeeValidatorTest {

public void validate(String schemaFile, String xmlFile) {
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(getSource(schemaFile));
Validator validator = schema.newValidator();
ValidationHandler handler = new ValidationHandler();
validator.setErrorHandler(handler);
validator.validate(getSource(xmlFile));

if (handler.errorsFound == true) {
System.err.println("Validation Error : " + handler.exception.getMessage());
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private Source getSource(String resource) {
return new StreamSource(EmployeeValidatorTest.class.getClassLoader().getResourceAsStream(resource));
}

private class ValidationHandler implements ErrorHandler{
private boolean errorsFound = false;
private SAXParseException exception;

public void error(SAXParseException exception) throws SAXException {
this.errorsFound = true;
this.exception = exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
this.errorsFound = true;
this.exception = exception;
}
public void warning(SAXParseException exception) throws SAXException {
}
}

/*
* Test
*/
public static void main(String[] args) {
new EmployeeValidatorTest().
validate("org/daz/xml/validation/employee.xsd", "org/daz/xml/validation/employee.xml");
}
}


related SO question: about "Cannot find the declaration of element "
http://stackoverflow.com/questions/3049779/cannot-validate-xml-doc-againest-cybs-xsd-schema-cannot-find-the-declaration-of/3049812#3049812
Post a Comment