XML Schema Validation with Axiom

Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.
  • By Eran Chinthaka
  • 4 Dec, 2007

Introduction

XML Validation in AXIOMAXIOM is one of the pioneering XML object models built on XML pull parsing. Also it is THE object model within Apache Axis2. (For an introduction to AXIOM, please read the article series AXIOM - Fast and Lightweight Object Model for XML on Oxygen tank.) It is often required to validate XML documents against a pre-defined set of rules. One way of defining these rules is to use an XML schema document. Even though AXIOM doesn't support schema validation natively, it does provide a mechanism to validate your XML documents with the help of javax.xml.validation utilities that come with JDK 1.5. This tutorial demonstrates how AXIOM can be used to perform a schema validation.

 

Applies To

Apache AXIOM/Java 1.2 or later
Java Version 1.5 or later

 

Table of Contents

(If you are not familiar with Apache AXIOM, it is highly recommended to read the AXIOM article series found within Oxygen tank. Some of those articles are listed in the Resources section.)

 

XML Validation

The javax.xml.validation package has the ability to validate a given XML with a given schema. This is a very handy tool and comes with JDK 1.5 and later versions. We will be using these utilities combined with AXIOM to do the schema validation.

Note : This code will not work in JDK 1.4 or earlier versions.

javax.xml.validation must be given an instance, org.w3c.Node, or a reference to the XML file for it to validate with a schema. But if we have an instance of an OMElement how do we do that?

There are a couple of ways to do it. The easiest and the obvious way to do it is to first convert the OMElement into either a java.lang.String or to a java.io.File and then feed that to the validator. This might be a bit cumbersome depending on the constraints of your environment. For example, if you are within Apache Axis2, and if you want to do this, then it might not be that easy to do it.

However there is an easy way of doing this. AXIOM has two different implementations of the same set of interfaces. One implementation uses a linked list model, and is used most of the time. The other implements AXIOM interfaces on top of a DOM object model. These two can be converted into each other easily. If you have an OMElement, which represents your XML fragment, this OMElement can be created using the linked list object model or DOM based object model. Irrespective of how it is created, let's see how we can use OMElements to do a schema validation with the help of javax.xml.validation utilities.

Setting up the Environment

You need to set up your environment with all the AXIOM jars and dependencies. If not, please download the AXIOM 1.2.5 version. Once you download and extract it, you will be able to see AXIOM jars inside the build folder and the dependencies it needs inside the jars folder.

You can try an example schema validation discussed in this tutorial by downloading it from here. (You need to have Apache Ant installed in your computer to run the samples.) Once you download and extract it, open the build.xml and set the locations of your AXIOM jars and the dependent libraries.

<!--set the location of your AXIOM jars and dependency libraries
here-->
<property name="axiom-libs" value="${AXIOM_HOME}/build"/>
<property name="axiom-dep-libs" value="${AXIOM_HOME}/lib"/>

Code Listing 1 - Configuring build.xml of the source code

You run the example by typing ant in the console. Now let's dig into the code and see what is happening.

Schema Validation with AXIOM

As we discussed earlier, we need to have an instance of org.w3c.Node to pass to the javax.xml.validation utility. So first let's convert our OMElement to an org.w3c.Element (org.w3c.Element extends from org.w3c.Node and it represents the XML element).

Element sourceElement; // if the OMElement is created using DOM implementation use it 
if (omElement instanceof ElementImpl) {
sourceElement = (Element) omElement;
} else { // else convert from llom to dom sourceElement
sourceElement = getDOMElement(omElement);
}

Code Listing 2 : Converting AXIOM OMElement in to org.w3c.Element

Again, as we discussed earlier, AXIOM has two implementations. If the OMElement that we have is based on the DOM based implementation, then we just have to cast it as an org.w3c.Element as it also implements DOM interfaces (Kudos to Ruchith Fernando for implementing it). If you have an instance of a linked list OMElement, then we need to convert it to a DOM based element. The getDOMElement method does just that. You might not want to worry about the details on this conversion at this point. You can read more information on these AXIOM conversions in the article at https://wso2.com/library/2512

Now the job of AXIOM is over. Now let's turn to real schema validation. You need to first get access to a schema factory that can understand XML schema documents. The selection of factories here helps you to validate your documents against various other schema definition languages like RelaxNG. For more information on using this validation with other schema languages, please refer to the article published in IBM developer works.

SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Code Listing 3 : Creating SchemaFactory

Now we need to load our schema document that we will be validating against. Create an instance of the StreamSource class with your schema file and then use it to create an instance of the Schema using the factory created above.

Source source = new StreamSource(schemaFile); 
Schema schema = factory.newSchema(source);

Code Listing 4 : Creating Schema from the validating schema file

Finally we want the validator. We create a validator from the schema we just created.

Validator validator = schema.newValidator();

Code Listing 5 : Creating the validator

We are ready to validate any XML documents against our loaded schema now. Let's pass the DOM element that we created in code listing 1 into the validator.

validator.validate(new DOMSource(sourceElement));

Code Listing 6 : Validating an XML document

That's it. The validator will notify you if there is an error in your XML.

public static void validate(OMElement omElement, File schemaFile) throws Exception { 

Element sourceElement; // if the OMElement is created using DOM implementation use it

if (omElement instanceof ElementImpl) {
sourceElement = (Element) omElement;
} else { // else convert from llom to dom
sourceElement = getDOMElement(omElement);
} // Create a SchemaFactory capable of understanding WXS schemas.

// Load a WXS schema, represented by a Schema instance.
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source source = new StreamSource(schemaFile);

// Create a Validator object, which can be used to validate
// an instance document.
Schema schema = factory.newSchema(source);
Validator validator = schema.newValidator();

// Validate the DOM tree.
validator.validate(new DOMSource(sourceElement));
}

public static void
validate (File sourceFile, File schemaFile) throws Exception {
// since we have the input source as a stream, let's directly create the DOOM elements

XMLStreamReader streamReader = XMLInputFactory.newInstance().createXMLStreamReader(new
FileInputStream(sourceFile));
validate(new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(),
streamReader).getDocumentElement(), schemaFile);
}

Code Listing 7 : The complete code to validate OMElement against a schema

Complete source code with the ant file and resources can be downloaded from here.

Summary

We looked at how AXIOM can be used to validate an XML document against a given schema. This will be very handy if you have already used AXIOM to represent your XML document. We used the validation capabilities of Java XML validation API, integrated into the Java Development Kit 1.5 to do this validation.

 

Resources

Download Apache Axiom

Download Apache Ant to build and run the example

Article on "Creating an AXIOM Object Model Using Different Input Sources" published on Oxygen Tank from the same author

"The Java XML Validation API" published on IBM developeWorks from Elliotte Rusty Harold

Author

Eran Chinthaka - Member Apache Software Foundation, Project Management Committee Member:Apache WS Project. chinthaka(!) at apache(!).org(!)

About Author

  • Eran Chinthaka
  • Software Engineer
  • WSO2 Inc.