Jenkov.com
Tutorials Books About
Java Xml

1 Java & XML Tutorial
2 Java & XML Tool Overview
3 Java SAX Parser
4 Java SAX DefaultHandler
5 Java SAX Schema Validation
6 Java SAX Parsing Example
7 Java SAX vs. StAX
8 Java StAX
9 Java StAX: XMLInputFactory
10 Java StAX: XMLOutputFactory
11 Java StAX: XMLEventReader - The Iterator API
12 Java StAX: XMLEventWriter - The Iterator Writer API
13 Java StAX: XMLStreamReader - The Cursor API
14 Java StAX: XMLStreamWriter - The Cursor Writer API
15 Java DOM
16 Java DOM: The Document Object
17 Java DOM Schema Validation




Java DOM Schema Validation


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!