2 Replies Latest reply: Jul 15, 2011 10:09 AM by jtahlborn RSS

    XML validation with StAX


      I have a requirement to validate XMLs during JAXB unmarshalling and during StAX parsing. The schema is packaged in a jar. I am able to successfully read the schema, and validate the XML during unmarshalling by setting the schema in the unmarshaller (through setSchema()).

      However, I am not able to validate it with StAX using the same schema and same XML.

      Here is the code snippet for StAX:

      StringReader stringReader = new StringReader(inputXMLStr);
      XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(stringReader);
      EventFilter filter = new EventFilter() {
      public boolean accept(XMLEvent event) {
      return event.isStartElement();
      XMLEventReader xmlFilteredEventReader = xmlif.createFilteredReader(xmlEventReader, filter);

      Schema mySchema = getSchema(); // this method retrieves the schema by reading the schema files as
      stream source and calling schemaFactory.newSchema(...)

      Validator validator = mySchema.newValidator();
      validator.setErrorHandler(new SchemaErrorHandler());
      Source xmlSource = new StAXSource(xmlFilteredEventReader);

      I get a SAX Parse exception saying "cvc-elt.1: Cannot find the declaration of element 'myElement'"

      And I also see the following exception:
      Caused by: java.lang.NullPointerException
           at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.bridge(StAXEvent2SAX.java:171)
           at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.parse(StAXEvent2SAX.java:118)
           at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:651)
           at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
           at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
           at com.sun.org.apache.xerces.internal.jaxp.validation.StAXValidatorHelper.validate(StAXValidatorHelper.java:89)
           at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:114)
           at javax.xml.validation.Validator.validate(Validator.java:127)
      I am not able to figure out why the validation would work with UnMarshaller but not with StAX.
      Any help appreciated.

        • 1. Re: XML validation with StAX
          Have u tried the Stream STAX parser instead?

          something like:

          SchemaFactory factory = SchemaFactory
          File schemaLocation = new File(XSD_FILE_PATH);
                         Schema schema = factory.newSchema(schemaLocation);

          // 3. Get a validator from the schema.
          Validator validator = schema.newValidator();
          ErrorHandler lenient = new ForgivingErrorHandler(fw);

          XMLInputFactory staxFactory = XMLInputFactory.newInstance();
          staxFactory     .setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
          FileInputStream fis = new FileInputStream(XML_FILE_PATH);

          XMLStreamReader xmlr = staxFactory.createXMLStreamReader(fis);

          validator.validate(new StAXSource(xmlr));
          • 2. Re: XML validation with StAX
            why are you filtering your stax events? i would imagine that would make it difficult to validate (as your filter is only passing through start element events).