Skip to end of metadata
Go to start of metadata

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:

Labels
  • None
  1. Feb 29, 2012

    hi,dude,i wanna ask some questions about WSTX's validation ability.

    i mean i hava got multiple xsd files,and wanna use WSTX to validate the xml files,but there is a problem that i 'm not sure that whether WSTX can support multiple validation xsd files as a group to validate xml files.here is my codes,only work when there is one xsd file.but my situtation is one xsd file linked to another one ,and so on.

    package samples;

    import java.io.File;

    import javax.xml.stream.*;

    import org.codehaus.stax2.*;
    import org.codehaus.stax2.validation.*;

    /**
    * This is a simple example command line utility, that shows how to
    * use the new Stax2 validation API to validate input documents against
    * specific DTD instance.
    */
    public class ValidateWithDtd
    {
    public static void main(String[] args)
    {
    if (args.length != 2) {
    System.err.println("Usage: java ValidateWithDtd [input-file] [dtd-file]");
    System.exit(1);
    }
    // First, let's parse DTD schema object
    XMLValidationSchemaFactory sf = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
    File schemaFile = new File(args[1]);
    ///File schemaFile2 = new File(args[2]);
    XMLValidationSchema xsd= null;
    ///XMLValidationSchema xsd2= null;

    try {
    xsd= sf.createSchema(schemaFile);
    // xsd2= sf.createSchema(schemaFile2);//can't work 
    } catch (XMLStreamException xe) {
    System.err.println("Failed to process the DTD file ('"+schemaFile+"'): "+xe);
    System.exit(1);
    }

    // And then validate a document:
    File inputFile = new File(args[0]);
    try {
    XMLInputFactory2 ifact = (XMLInputFactory2)XMLInputFactory.newInstance();
    XMLStreamReader2 sr = ifact.createXMLStreamReader(inputFile);

    try {
    sr.validateAgainst(xsd);
    // sr.validateAgainst(xsd2);// can 't work 
    /* Document validation is done as document is read through (ie.
    * it's fully streaming as well as parsing), so just need to
    * traverse the contents.
    */
    while (sr.hasNext()) {
    sr.next();
    }
    } catch (XMLValidationException vex) {
    System.err.println("Document '"+inputFile+"' failed validation: "+vex);
    System.exit(1);
    }
    } catch (XMLStreamException xse) {
    System.err.println("Failed parse the input document ('"+inputFile+"'): "+xse);
    System.exit(1);
    }
    System.out.println("Document '"+inputFile+"' succesfully validated.");
    }
    }

    it seems that WSTX's API don't support multiple xsd&dtd files as a group to validate xml files.

    please help me,thank you!!!

    you'd better send me in the email:shuoleo@126.com

    thank you !!!!