|
| | | | 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:
-
Create the DOMParser.
-
Retrieve
DOMConfiguration from the Document ,
and set validate feature to true.
-
Provide XML Schemas (agains which validation should occur)
by either setting xsi:schemaLocation /
xsi:noSchemaLocation attributes on the
documentElement , or
by setting schema-location parameter on the
DOMConfiguration .
-
Relative URIs for the schema documents will be resolved relative to the
documentURI
(which should be set).
Otherwise, you can implement your own
LSResourceResolver and set it
via resource-resolver on the DOMConfiguration .
Note: if a document contains any DOM Level 1 nodes (the nodes created using createElement,
createAttribute, etc.) a 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.DOMConfiguration;
import org.w3c.dom.ls.LSParser;
.....
Document document = builder.parseURI("data/personal-schema.xml");
DOMConfiguration config = document.getDomConfig();
config.setParameter("error-handler",new MyErrorHandler());
config.setParameter("validate", Boolean.TRUE);
document.normalizeDocument(); | | | | |
For more information, please refer to the
DOM Level 3 Implementation
page.
|
|