Java & XML
Java & XML Introduction
Java & XML Tool Overview
 
Java SAX
Java SAX DefaultHandler
Java SAX Schema Validation
Java SAX Example
Java SAX vs. StAX
 
Java StAX
Java StAX XMLInputFactory
Java StAX XMLOutputFactory
Java StAX XMLEventReader
Java StAX XMLEventWriter
Java StAX XMLStreamReader
Java StAX XMLStreamWriter
 
Java DOM Parser
Java DOM Document Object
Java DOM Schema Validation
 


Java DOM Schema Validation

Connect with me:  
  Jenkov.com - News

In Java it is possible to validate a DOM graph according to an XML Schema. The technique I will show here is validation after the DOM graph is created, not during creation as was otherwise the norm with DTD validation.

First you must load the XML Schema into a Schema object. Once loaded into a Schema object you can use that same Schema object to validate multiple DOM graphs. This is smart, because then you only have to parse the XML Schema once. The result is significantly better performance, compared to both loading and parsing the XML document and the XML Schema / DTD for each XML document, as was the case with the previous DTD validation mechanisms.

Here is how you load an XML Schema into a Schema instance:

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) {
    e.printStackStrace();
}

There are a few additional newSchema() methods, for instance one that lets you load a Schema from a Java URL object.

Once loaded, you validate the DOM document like this:

Validator validator = schema.newValidator();

validator.validate(new DOMSource(document));

The Schema classes are found in the java package javax.xml.validation. The DOMSource classes are found in the java package javax.xml.transform.

If the validation fails, an exception is thrown. You can also set an error handler on the Validator object. Thus, you can collect multiple schema errors in the same document.

Connect with me:
     
Newsletter - Get all my free tips!