Java SAX Schema Validation

Jakob Jenkov
Last update: 2014-05-21

It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it looks:

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();
}


SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setSchema(schema);

SAXParser parser = spf.newSAXParser();

parser.parse(...);

First a Schema object is created from some XML schema file.

Second, a SAXParserFactory is created and this factory has the Schema instance set on it. All SAXParser instances created by this factory will use this XML Schema to validate XML files.

Third, an XML file is parsed and validated.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















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
Previous
Next tutorial in this tutorial trail
Next