Thursday, 26 July 2007

quicky: xml schema validation using dom4j

I believe I have spend to much time with xml schema validation using dom4j, considering what I think should be a trival operation.
I was not capable of finding the howto on this subject, so I will provide my own. Mosty as a note to myself the next time I have to do schema validation.

public Document parseXMl(String xml) throws Exception {
URL resource = getClass().getResource("/my-schema.xsd");
SAXReader reader = new SAXReader(true);
reader.setFeature("", true);
reader.setProperty("", "");
reader.setProperty("", new File(resource.getFile()));
reader.setErrorHandler(new ErrorHandlerImpl());
InputSource source = new InputSource(new StringReader(xml));


Anonymous said...

Brilliant, thank you. I too spent way too long looking for the answer to this seemingly trivial question.

Jacob von Eyben said...

One other thing. If the schema you try to use is placed in one of your depending jar files you cannot just do:
reader.setProperty("", new File(resource.getFile()));

Then you have to create your own entity resolver.
Inspired by this thread:$e7bcadb0$36a4c8c3@SHAREDVALUE.COM%3E

I created my own entity resolver:

private class CachedEntityResolver implements EntityResolver {

private String systemId;
private InputStream stream;

public CachedEntityResolver(String systemId, InputStream stream) {
this.systemId = systemId; = stream;

public InputSource resolveEntity(String publicId, String systemId) {
InputSource result = null;
if (systemId.contains(this.systemId)) {
result = new InputSource(stream);
return result;

and used it like this:

schemaStream = ReportParserImpl.class.getResourceAsStream(ATLIGHT_REPORT_SCHEMA_SOURCE);
reader.setEntityResolver(new CachedEntityResolver("schema-name.xsd", schemaStream));

Then my document header looks like this:
<rapport xmlns:xsi="" xsi:noNamespaceSchemaLocation="schema-name.xsd">

I'm not sure that the fact that I just check if the systemId contains 'schema-name.xsd' is a hack, but it works and is deployable in any environment as it doesn't take the domain into concern.

Koos Pol said...

Thanks. After lots of time digging, conflicting documents, unclear apis, yours was spot on. Thanks a milllion.