How to use Stax2 Validation API
What is it?
Stax2 Validation API is defined under org.codehaus.stax2.validation package, and implemented by Woodstox 3.0 and above (including pre-3.0 release candidates).
It is an API that defines a bi-directional (both reader and writer side) extensible, pluggable (allows external implementation), dynamic (can start and stop validation during parsing) and powerful (allows validation using more than one validator) validation. Woodstox 3.0 contains implementations for two standard validation languages (DTD, Relax NG), and in future hopefully more (at least W3C Schema, and perhaps some subset of Schematron?).
Is there example code available?
Yes. Example code can be found from under src/samples in the source repository (and source distribution). Existing classes cover various aspects of validation as follows:
- Simple reader-side full-file validation:
- src/samples/ValidateWithDtd.java implements a simple command-line DTD-based validator
- src/samples/ValidateWithRelaxNg.java implements a simple command-line RelaxNG-based validator (using Sun's MSV validator – note: need MSV jars for this sample)
- And similar simple validation for output (XMLStreamWriter):
- src/samples/ValidateOutputWithDtd.java shows how to validate XML output as it gets written.


