|
 |  |  |  | How can I make sure that my DOM document in memory conforms to a schema? |  |  |  |  |
| |
DOM revalidation is supported via W3C DOM Level 3 Core
Document.normalizeDocument().
.
 | This release only supports revalidation against XML Schemas. Revalidation against DTDs or any other schema type is not implemented. |
To revalidate the document you need:
-
Build DOM Level 3 Xerces jars.
-
Create the DOMBuilder.
-
Set validate feature using
setNormalizationFeature
method.
-
Make sure your document has xsi:schemaLocation
or
xsi:noSchemaLocation attributes at the document root that
specify the location of schema(s) against which validation should occur.
-
The
documentURI
must be set. Locations of the schema documents will be resolved relative to the
documentURI
.
Note: if a document contains any DOM Level 1 nodes (the nodes created using createElement,
createAttribute, etc.) the fatal error will occur as described in the
Namespace Normalization
algorithm.
In general, the
DOM specification
discourages using DOM Level 1 nodes in the namespace aware application:
DOM Level 1 methods are namespace ignorant. Therefore, while it is safe to use these methods when not
dealing with namespaces, using them and the new ones at the same time should be avoided. DOM Level 1 methods
solely identify attribute nodes by their nodeName. On the contrary, the DOM Level 2 methods related to namespaces,
identify attribute nodes by their namespaceURI and localName. Because of this fundamental difference, mixing both
sets of methods can lead to unpredictable results.
 |  |  |  |
import org.w3c.dom.Document;
import org.w3c.dom.ls.DOMBuilder;
.....
Document document =
builder.parseURI("data/personal.xml");
document.setErrorHandler(new MyErrorHandler());
document.setNormalizationFeature("validate", true);
document.normalizeDocument();
|  |  |  |  |
For more information, please refer to the
DOM Level 3 Implementation
page.
|
|