Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] XML Schema validation via JAXP

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Peter Stibrany" <pstibrany AT gmail.com>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] XML Schema validation via JAXP
  • Date: Sat, 11 Aug 2007 16:57:37 +0200

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




Archive powered by MHonArc 2.6.24.

Top of Page