[XOM-interest] XML Schema validation via JAXP
Peter Stibrany
pstibrany at gmail.com
Sat Aug 11 10:57:37 EDT 2007
Hello,
I use XOM together with JAXP to perform parsing and schema validation
of xml document:
public static Document parse(InputStream documentInputStream)
throws SAXException, ParserConfigurationException, ValidityException,
ParsingException, IOException {
// read schema
InputStream xsdSchema =
Test.class.getResourceAsStream("searchRequest.xsd");
StreamSource ss = new StreamSource(xsdSchema);
// create schema object
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(ss);
// create sax parser, and configure it
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
spf.setSchema(schema);
SAXParser parser = spf.newSAXParser();
// build XOM Document
Builder builder = new Builder(parser.getXMLReader(), false);
Document doc = builder.build(documentInputStream);
return doc;
}
(StreamSource, SchemaFactory, Schema, SAXParserFactory and SAXParser
are from javax.xml packages)
This works for me in Java 1.5, where it throws ParsingException when
document isn't valid. Unfortunately, on Java 6 it returns parsed
document even if it is invalid with respect to schema.
Before I start investigating problem, I'd like to be assured, that
above way of parsing document with schema validation is correct.
Thank you for your support,
-Peter
More information about the XOM-interest
mailing list