View RSS Feed

Java XML

Java SAX Schema Validation

Rate this Entry
by , 04-27-2012 at 06:16 PM (1183 Views)
It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it looks:
Java Code:
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) {

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser parser = spf.newSAXParser();

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. AllSAXParser instances created by this factory will use this XML Schema to validate XML files.
Third, an XML file is parsed and validated.

Submit "Java SAX Schema Validation" to Facebook Submit "Java SAX Schema Validation" to Digg Submit "Java SAX Schema Validation" to Submit "Java SAX Schema Validation" to StumbleUpon Submit "Java SAX Schema Validation" to Google

Tags: sax schema Add / Edit Tags