Friday, November 19, 2010

XML Document validation with XML Schema

In todays blogpost, I will show you how to validate an XML-document to its XSD-schema. Before we begin, create an example XML-schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://javaeenotes.blogspot.com/schema"
xmlns:tns="http://javaeenotes.blogspot.com/schema"
elementFormDefault="qualified">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="birthdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

You can use this schema to generate an XML-document with Eclipse:

<?xml version="1.0" encoding="UTF-8"?>
<tns:person
xmlns:tns="http://javaeenotes.blogspot.com/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://javaeenotes.blogspot.com/schema schema.xsd">
<tns:name>Albert</tns:name>
<tns:age>29s</tns:age>
<tns:birthdate>1981-11-19</tns:birthdate>
</tns:person>

Now, the last thing we need to do, is to build the validator:

package com.javaeenotes;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Main {
private static final String SCHEMA = "schema.xsd";
private static final String DOCUMENT = "document.xml";

public static void main(String[] args) {
Main m = new Main();
m.validate();
}

public void validate() {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);

try {
DocumentBuilder parser = dbf.newDocumentBuilder();
Document document = parser.parse(new File(DOCUMENT));
SchemaFactory factory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = null;
schema = factory.newSchema(new File(SCHEMA));
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (SAXException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}

The validator will throw an exception if the validation process fails. Based on the specific type of error, the following exceptions could be thrown:

  • SAXException: XML-document not valid.

  • IllegalArgumentException: XML-artifact/instruction not supported.

  • IOException: document/schema file read error.

  • ParserConfigurationException: parser could not be configured.

No comments:

Post a Comment