Thursday, June 04, 2009

Digester, validation and XSD

How to validate XML using XSD in Digester (1.8 version)? There is no clear answer in Digester documentation.
Here you can see how to validate XML using XSD:

File xsdFile = new File(${location});
Digester digester = new Digester();
digester.setValidating(true);
digester.setNamespaceAware(true);
digester.setErrorHandler(myErrorHandler); // Don't forget to set custom error handler
digester.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
digester.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", xsdFile); // You can use File, String, InputStream, InputSource or an array of these type
digester.parse(...);