Skip to main content

JAXP validation api doesn't work with StAXSource?

4 replies [Last post]
bighurtt
Offline
Joined: 2007-04-27
Points: 0

Am I doing something wrong? I am attempting to create javax.xml.transform.stax.StAXSource object and pass it to a schema validator object and then call validate() but I get the following stack trace output when I run it:

ERROR: ''
Exception in thread "main" java.lang.NullPointerException
at com.sun.org.apache.xerces.internal.jaxp.validation.StAXValidatorHelper.validate(StAXValidatorHelper.java:95)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:114)
at javax.xml.validation.Validator.validate(Validator.java:127)
at alex.test.StaxExample.methodA(StaxExample.java:43)
at alex.test.StaxExample.main(StaxExample.java:22)

The schema .xsd file as well as the data .xml file are found and are good. On paper by reading the API's this should work. . .but it doesn't appear that it actually does. Here is the sample code, and I know I am not the only person who has encountered this problem as I took this from another forum elsewhere. But the post was somewhat old and I found nothing as far as a solution or answer by googling around for a while.

package alex.test;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class StaxExample {

public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage : StaxExample <xmlFile> <schemaFile>");
} else {
methodA(args[0], args[1]);
}
}

private static void methodA(String xmlFile, String xsdFile)
throws Exception {
XMLInputFactory xmlif = XMLInputFactory.newInstance();

XMLStreamReader staxReader = xmlif.createXMLStreamReader(new StreamSource(xmlFile));

SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaGrammar = schemaFactory.newSchema(new File(xsdFile));

Validator schemaValidator = schemaGrammar.newValidator();

Source staxSrc = new StAXSource(staxReader);
schemaValidator.validate(staxSrc);

while (staxReader.hasNext()) {
int eventType = staxReader.next();
System.out.println("Event of type: " + eventType);
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joehw
Offline
Joined: 2004-12-15
Points: 0

It looks like there's a validation error but a bug in StAXValidatorHelper prevented it from reporting the error properly.

Here's a workaround: use the validate method with a StAXResult:

Result staxResult = new javax.xml.transform.stax.StAXResult(
XMLOutputFactory.newInstance().createXMLStreamWriter(
new FileWriter(resultFile)));
schemaValidator.validate(staxSrc, staxResult);

I will create a report for the bug.

bighurtt
Offline
Joined: 2007-04-27
Points: 0

Hey thanks! I'm glad somebody else thinks it is a real bug too and not just me. As far as it being a validation error, I really don't think so. The schema and xml data files I was using were well formed and valid to the best of my knowledge. I was using the files personal.xsd and personal-schema.xml as my test data, both of which come in the samples/data directory of the JAXP nightly build download. I just assumed both of those were "good" files. But anyway, thanks for the workaround suggestion. I will try it out right away!

Message was edited by: bighurtt

joehw
Offline
Joined: 2004-12-15
Points: 0

You're right. The bug exists no matter if the xml is valid. I've filed a bug report based upon your post:

http://bugs.sun.com/view_bug.do?bug_id=6708840

Thanks,
Joe

joehw
Offline
Joined: 2004-12-15
Points: 0

The bug has now been fixed in JAXP 1.4 on java.net. I will make a request to integrate the change into JDK6 update release. Will update the bug and this thread when I know a possible release. Thanks.