View RSS Feed

Java XML

Java DOM Schema Validation

Rate this Entry
by , 04-27-2012 at 05:12 PM (751 Views)
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:
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) {
    e.printStackStrace();
}

Submit "Java DOM Schema Validation" to Facebook Submit "Java DOM Schema Validation" to Digg Submit "Java DOM Schema Validation" to del.icio.us Submit "Java DOM Schema Validation" to StumbleUpon Submit "Java DOM Schema Validation" to Google

Tags: dom schema Add / Edit Tags
Categories
DOM

Comments