Friday, August 04, 2006

Using Stax2 (Woodstox 3.0) Validation API, part 2

Ok, first things first: the code sample I will go through can be found from http://woodstox.codehaus.org/DocStax2Validation, and will be part of Woodstox source code distribution (in src/samples/).

So here is the basic usage pattern for using Stax2 validation API on reader side. Example will validate a document read via XMLStreamReader, but same could easily be done by with XMLEventWriters: you just need to first construct the stream writer, and then event writer using that specific stream reader. Order of attack is as follows:

  1. Get an instance of XMLValidationSchemaFactory that knows how to parse schemas of the type you need (RelaxNG == rng for this example).
  2. Ask factory to construct a XMLValidationSchema, given a resource (file, URL, InputStream, Reader): it will parse the resource as necessary.
  3. Construct your Stax stream reader as usual
  4. Enable validation using schema you got from step 2
  5. Traverse the input document using stream reader -- this is necessary, since validation is done in fully streaming manner.
  6. There is no step 6!

Sound simple enough? Ok, here is the source code (minus comments, error handling and class declaration of the actual sample class -- for full class, see the link above), with comments indicating where each step starts:

// step 1: get schema factory
XMLValidationSchemaFactory sf = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_RELAXNG);
// step 2: construct validation schema instance
XMLValidationSchema rng = sf.createSchema(new File(args[1]));
// step 3: construct stream reader
XMLInputFactory2 ifact = (XMLInputFactory2)XMLInputFactory.newInstance();
XMLStreamReader2 sr = ifact.createXMLStreamReader(new File(args[0]));
// step 4: enable validation
sr.validateAgainst(rng);
// step 5: stream through the document:
while (sr.hasNext()) {
  sr.next();
}
// done!
   

And there you have it: simple validation of an xml document, against an Relax NG Schema. The exact same procedure (except for getting a different schema validation factory in step 1) would work for other types, specifically for DTDs. And in future, with other pluggable schema factories, for other schemas like W3C Schema as well.

Now that you know how to do this simple task, possible next tasks would be:

  1. Validating XML document you are writing against a Schema: not surprisingly, code looks very similar to above. In fact, you only need to change steps 3 and 5!
  2. Validating a single document against multiple schemas. Just repeat steps 2 and 4 multiple times!
  3. Writing your own custom schema validators, to separate business level data validation from access.
  4. Validating sub-trees, possibly against different schemas.

I will try to find time to write about some of above ideas in near future. Stay tuned!

blog comments powered by Disqus

Sponsored By


Related Blogs

(by Author (topics))

Powered By

About me

  • I am known as Cowtowncoder
  • Contact me at@yahoo.com
Check my profile to learn more.