Monday, January 11, 2010

XML Validation

Categories:



Validating XML Documents Against XML Schema
By Deepak Vohra
Learn the three methods involved: SAX parser, DOM parser, and XSD validator
XML documents are increasingly being used as a format of data exchange. But for an XML document to be acceptable to different developers/users, the XML document should conform to a standard structure.
XML Schema is an XML-based representation of the structure of an XML document. Through its support for datatypes and namespaces, XML Schema has the potential to provide the standard structure for XML elements and attributes.
However, to check if an XML document conforms to an XML Schema, the document must be validated against that XML Schema. This tutorial explains the procedure for performing that validation using parsers for the Simple API for XML (SAX) and Document Object Model (DOM), as well as an XML Schema Design (XSD) validator. Table 1 compares the different purposes of each of these components. In each case, we will use the Oracle XML Developer's Kit (XDK) 10g Production to perform the validation.

SAXParserDOMParserXSDValidator
used for validating an XML document if parsing is requiredused for validation if the XML document is required to be parsed and a DOM structure of the XML document is to be generatedused for validation of the XML document when parsing is not required

Preliminary Setup
To validate an XML document with the Oracle XDK, parser classes are required in the Java CLASSPATH. First, extract the xdk_nt_10_1_0_1_0A_beta.zip file to an installation directory. Add /lib/xmlparserv2.jar, /lib/xml.jar , and /lib/xschema.jar - to the Java CLASSPATH, where is the directory in which Oracle XDK is installed.
Document to Validate
Before validating an XML document, you must specify the location of the XML Schema.
The location of the XML Schema can be specified with the xsi:schemaLocation attribute for an XML Schema with namespaces, and the xsi:noNamespaceSchemaLocationattribute for an XML Schema without namespaces, in the root/top-level element, or in any other element of the XML Document. In our case, we'll use the example of an XML document, XmlDocumentUrl, that contains an Oracle Magazine catalog.
If the schema location is set in the XML document, the element is defined as



The example XML document is shown in Listing 1.
Validation XML Schema
As I explained previously, an XML Schema—in our example, SchemaUrl—defines the structure of an XML document. The element declarations in an XML Schema can specify the namespace/s of the elements in the XML document. A namespace is defined with the notation xmlns:prefix=prefix is the namespace prefix; the default namespace is specified with xmlns=. The xs:schema element in our example schema document OracleCatalog.xsd is specified with a namespace declaration as



The elements in the OracleCatalog.xsd XML Schema document (see Listing 2) are in the XML Schema namespace of http://www.w3.org/2001/XMLSchema.
Validation of XML Document with SAXParser
Next, we need to validate. First, import the SAXParser class:

import oracle.xml.parser.v2.SAXParser;
Create a SAXParser:
SAXParser saxParser=new SAXParser();
Set the validation mode to report validity errors:
saxParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
Now set the XML Schema for validation:
XSDBuilder builder = new XSDBuilder();
URL url =  new URL(SchemaUrl);      
XMLSchema schemadoc = (XMLSchema)builder.build(url);
saxParser.setXMLSchema(schemadoc);
Create and register an ErrorHandler with the SAXParser parser. The ErrorHandler class is the Validator class.
Validator handler=new Validator();
saxParser.setErrorHandler(handler);

Parse the XML document to validate with the XML schema.
saxParser.parse(XmlDocumentUrl);
Any errors generated by the parser are registered with the ErrorHandler and may be retrieved from the ErrorHandler.
The example program, XDKValidator.java, used to validate OracleCatalog.xml with OracleCatalog.xsd and the SAXParser API is shown in Listing 3.
Validation with DOMParser
Now, let's validate our XML document with the DOMParser. First, import the DOMParser class:

import oracle.xml.parser.v2.DOMParser;

Create a DOMParser:

DOMParser domParser=new DOMParser();  

Set the validation mode to report validity errors:

domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);

Set the XML Schema used for validation:

XSDBuilder builder = new XSDBuilder();
URL url =  new URL(SchemaUrl);      
XMLSchema schemadoc = (XMLSchema)builder.build(url);
domParser.setXMLSchema(schemadoc);

Create and register an ErrorHandler with the DOMParser parser. The ErrorHandler class is the Validator class:

Validator handler=new Validator(); 
domParser.setErrorHandler(handler);

Parse the XML document to validate with the XML schema:

domParser.parse(XmlDocumentUrl);

Any errors generated by the parser are registered with the ErrorHandler and may be retrieved from the ErrorHandler. The example program used here, DOMValidator.java, is shown in Listing 4.
Validation with XSDValidator
Import the XSDValidator class:

import oracle.xml.schemavalidator.XSDValidator;

Create an XSDValidator:

XSDValidator xsdValidator=new XSDValidator();

Specify an XML Schema with which to validate the XML document:

XSDBuilder builder = new XSDBuilder();
 URL url =  new URL(schemaUrl);       
 XMLSchema schemadoc = (XMLSchema)builder.build(url);
xsdValidator.setSchema(schemadoc);

Create and register an ErrorHandler with the XSDValidator validator. The ErrorHandler class is the Validator class:




Validator handler=new Validator();
XMLError xmlError=new XMLError();
xmlError.setErrorHandler(handler); 
xsdValidator.setError(xmlError);
Parse the XML document you want to validate with the XML Schema:
xsdValidator.validate(new URL(XMLDocumentUrl));
As before, any errors generated by the parser are registered with the ErrorHandler and may be retrieved there. The example program used here, SchemaValidator.java, is shown in Listing 5. Validate Away You've just learned three different methods for validating an XML doc against XML Schema.  One of them should meet your needs.


Spread The Love, Share Our Article

Related Posts

No Response to "XML Validation"

Post a Comment