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));