DOM Level 3 Validation

DOM Level 3 provides a detailed API for validation. This API can be used to validate against any schema language the parser supports, although DTDs and W3C XML Schema Language schemas are certainly the most common options.


This section is based on working drafts of the relevant specifications and experimental software. The broad picture presented here is correct, but a lot of details are likely to change before DOM Level 3 is finalized.

Unlike SAX, DOM objects can be validated when the document is first parsed or at any later point. You can also validate individual nodes rather than validating the entire document. To validate while parsing, you set the following features on the document or document builder's DOMConfiguration object.

  • schema-type : A URI identifying the schema language used to validate. Values include for the W3C XML Schema Language and for DTDs.

  • schema-location : A white-space -separated list of URLs for particular schema documents used to validate.

  • validate : If true, all documents should be validated. If false, no documents should be validated unless validate-if-schema is true.

  • validate-if-schema : Validate only if a schema (in whatever language) is available, either one set by the schema-location and schema-type parameters or one specified in the instance document using a mechanism such as a DOCTYPE declaration or an xsi:schemaLocation attribute.

For example, here's the DOM Level 3 code to parse the document at while validating it against the schema at .

 DOMImplementation impl = DOMImplementationRegistry    .getDOMImplementation("XML 1.0 LS-Load 3.0"); if (impl == null  !impl.hasFeature("Core", "3.0") {   throw new Exception("DOM Level 3 not supported"); } DOMImplementationLS implLS = impl.getInterface("LS-Load", "3.0"); DOMBuilder builder = implLS.createDOMBuilder(   DOMBuilder.MODE_SYNCHRONOUS,   ""); DOMConfiguration config = builder.getConfig(); config.setParameter("validate", Boolean.TRUE); config.setParameter("schema-location",   ""); config.setParameter("schema-type",   ""); builder.setErrorHandler(new DOMErrorHandler() {   public boolean handleError(in DOMError error) {     System.err.println(error.getMessage());   } }); Document doc = builder.parseURI(   ""); 

Currently, this API is only experimentally supported by Xerces and the Xerces-derived XML for Java, but more parsers should support it in the future.

If you make modifications to a document, DOM3 allows you to revalidate it to make sure it's still valid. This is an optional feature, and not all DOM Level 3 implementations support it. If one does, each Document object will be an instance of the DocumentEditVal interface as well. Just cast the object to this type and invoke the validateDocument() method as shown below.

 if (doc instanceof DocumentEditVal) {   DocumentEditVal docVal = (DocumentEditVal) doc;   try {     boolean valid = docVal.validateDocument();   }   catch (ExceptionVAL ex) {     // This document doesn't have a schema   } } 

You can even continuously validate a document as it is modified. If any change makes the document invalid, the problem will be reported to the registered DOMErrorHandler . Just set the continuousValidityChecking attribute to true.


This is particularly useful if the modifications are not driven by the program but by a human using an editor. In this case, you can even check the data input for validity before allowing the changes to be made.

If you need to change the schema associated with a document, set the schema-location and schema-type parameters on the document's DOMConfiguration object.

 DOMConfiguration config = doc.getConfig(); config.setParameter("schema-type",                     ""); config.setParameter("schema-location",                     ""); 

To validate this document, you would then call validateDocument() as described above.

Validation with DOM differs from validation with SAX in that you don't actually begin working with the document until after it has been validated. Thus there's no need to worry about committing the data in pieces. This is a common difference between SAX and DOM programs. A second advantage is that DOM validation can be reversed so that you build the document in memory and then check for validity before outputting it. You can even check every node you add to the Document object for adherence to a schema immediately and automatically.

Whether you validate with SAX or DOM, whether you do so continuously or just once when the document is first parsed, and whether the schema is a DTD, a W3C XML Schema Language schema, or something else, validation is an extremely useful tool. Even if you don't reject invalid documents, you can still use the result of validity checking to determine what to do with any given document. For instance, you might validate documents against several known schemas to identify the document's type and dispatch the document to the method that processes that type. Validation is an essential component of robust, reliable systems.

Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144 © 2008-2017.
If you may any questions please contact us: