Wednesday, December 10, 2008

Validating XML document using XML schema

The simplest way to validate an XML document is to use a Validator object. This object will perform a validation against the Schema object from which the Validator was created. Schema objects are typically created from SchemaFactory objects. The static newInstance() object allows you to create a SchemaFactory using a preset XML schema. The following code demonstrates this:

SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("mySchema.xsd"));
Validator validator = schema.newValidator();


Calling the validate() method on the Validator object performs the actual validation. This method takes at least a javax.xml.transform.Source object, of which you can use a SAXSource or a DOMSource, depending on your preference.

DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("myXMLDocument.xml"));
validator.validate(new DOMSource(document));


Here is a simple source example that shows how to validate an XML document using a World Wide Web Consortium (W3C) XML Schema, sometimes referred to as WXS.

try {
// Parse an XML document into a DOM tree.
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("myXMLDocument.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
Source schemaFile = new StreamSource(new File("mySchema.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate
// an instance document.
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e) {
// exception handling
} catch (SAXException e) {
// exception handling - document not valid!
} catch (IOException e) {
// exception handling
}




following code shows how one can validate xml document programtically against the schema


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;


public void validateXML() {

// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("instance.xml"));

// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(new File("mySchema.xsd"));
Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree
try {
validator.validate(new DOMSource(document));
} catch (SAXException e) {
// instance document is invalid!
}


}

No comments: