Tutorials About

Java DOM Schema Validation

Last updated: 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.

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

Close TOC
This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.