Java DOM Schema Validation

Jakob Jenkov
Last update: 2014-05-21

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) {

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.

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction


Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Next tutorial in this tutorial trail