Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using DOM 3.0 Validation Techniques with XDK 10g

DOM 3.0 validation techniques let you discover which elements and attributes you can add or remove from an XML document, ensuring in advance that planned modifications won't alter the overall validity of the document—all without revalidating the entire document.


advertisement
he DOM 3.0 Validation specification provides for validation of an XML document with a DTD or an XML Schema. The advantage of DOM 3.0 Validation over validation with a validating parser such as the DOMParser is the support for dynamic validation. The DOM 3.0 Validation specification implementation retrieves the metadata definitions from the XML Schema and provides methods to query the validity of DOM operations—for example the addition and removal of attributes and elements—so that you can validate potential document modifications before actually making them.

This article discusses how to use the DOM 3.0 validation implementation classes in XDK 10g Production using an example XML document called catalog.xml (see Listing 1), and showing how to validate it with an example XML Schema document called catalog.xsd (see Listing 2). Copy both the catalog.xml and catalog.xsd files to the same directory, for example, C:/DOM3Validation. XDK 10g is the only XML API which implements the DOM Level 3 Validation specification; however, some of the DOM 3Validation features are not implemented by the Oracle XDK 10g API. A test was run by W3C to determine which of the DOM3 Validation methods are supported by the XDK implementation. You can refer to the results of the DOM3 Validation test when developing a DOM 3 Validation application.

Preliminary Setup
To validate an XML document with the DOM 3.0 Validation API you need to add the org.w3c.dom.validation package classes to your classpath. First, download XDK 10g. Extract the downloaded file xdk_nt_10_1_0_2_0_production.zip to a directory. Add the /lib/xmlparserv2.jar and /lib/xschema.jar files to your classpath, where is a placeholder for the directory in which you installed XDK 10g Production.

Document Node Validation
In XDK 10g Production, the DOM 3.0 Validation specification is implemented with the DocumentEditVAL, ElementEditVAL, and NodeEditVAL interfaces. As the names indicate, you use the three interfaces to dynamically validate a document node—that is, the entire document, an element, or any individual node, respectively.

To validate a document, you use the DocumentEditVAL class. First, import the DOM 3.0 Validation specification classes and the load and save (LS) specification classes.

import org.w3c.dom.validation.*. import org.w3c.dom.ls.*;

Create an LSParser instance to parse an XML document. To create a LSParser, first create an XMLDOMImplementation object. Subsequently, create a LSParser object from the XMLDOMImplementation object using the createLSParser(short, String) method. The parameter of type short specifies the parsing mode: MODE_SYNCHRONOUS or MODE_ASYNCHRONOUS. If the mode is set to MODE_SYNCHRONOUS, as in this tutorial, the parse() and parseURI() methods of the LSParser object return an org.w3c.dom.Document object. If the mode is set to MODE_ASYNCHRONOUS the parse() and parseURI() methods return null. The String parameter of the createLSParser() method specifies the schema type. For an XML schema, the schema type value is http://www.w3.org/2001/XMLSchema.

XMLDOMImplementation impl = new XMLDOMImplementation(); LSParser parser = impl.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");

Parse the example XML document catalog.xml using the parseURI(String uri) method



XMLDocument document = (XMLDocument)( parser.parseURI( "file://C:/DOM3Validation/catalog.xml"));

You set the XML schema for validating the XML document using an XMLSchema object, which you can obtain from an XSDBuilder object using the build(URL schema) method. The URL parameter specifies the URL for the XML schema. Here's an example of creating an XMLSchema object.

XSDBuilder builder = new XSDBuilder(); URL url = new URL( "file://c:/DOM3Validation/catalog.xsd"); XMLSchema schemadoc = (XMLSchema)builder.build(url);

Set the schema document you want to use to validate the XMLDocument.

document.setSchema(schemadoc);

Create a DocumentEditVAL object from the XMLDocument object by casting the XMLDocument object to DocumentEditVAL.

DocumentEditVAL documentEditVAL=( DocumentEditVAL)document;

Set the validity checking of the DocumentEditVAL document using the method setContinuousValidityChecking(boolean continuousValidityChecking). If you pass true, the validity of the XML document with the XML schema is continuously checked. If set to false (the default value) the XML document's must be checked with a validation method.

documentEditVAL.setContinuousValidityChecking(true);

Validate the XML document catalog.xml with the XML schema catalog.xsd.

short valid=documentEditVAL.validateDocument();

There are three possible return values. If the XML document is valid, the value of the valid variable will be VALID_TRUE; otherwise, it will be VALID_FALSE. If the validity of the XML document is not known, it will be VAL_UNKNOWN.

For the example XML document and XML Schema document the output is

Document is VALID_TRUE

Global Element Declarations
You can obtain the global element declarations in an XML schema to determine which elements, except the root element, may be added to an XML document. A global element is a top level element in an XML schema declared with xsd:element element. Global elements in an XML document are obtained using the getDefinedElements(String namespaceURI) method, for example:

NameList elementList = documentEditVAL.getDefinedElements(null);

Using the sample XML Schema, the elementList would be filled with:

Element at index 0 is article Element at index 1 is journal Element at index 2 is catalog

The elements title and author in the XML Schema do not get listed, because they aren't global elements.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap