Validates the XML document against the specified XSD document.
XSD document are “XML Schema” that describe the structure of a XML document. The validator identify given XML file must be parsable using a DOM/SAX parser, and only then it will validate your XML against the XML Schema. The validator reports all error.
Here is example :-
//This method will Return all error in document List findProblems(File xml, File xsd) { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(xsd)) .newValidator().with { validator -> List exceptions = [] Closure<Void> handler = { exception -> exceptions << exception } errorHandler = [warning: handler, fatalError: handler, error: handler] as ErrorHandler validate(new StreamSource(xml)) exceptions } }
We can use this method like this .
def xyzXMLParser() { File xml = new File(this.class.classLoader.getResource('data/xyz.xml').path) File xsd = new File(this.class.classLoader.getResource('data/testing.xsd').path) findProblems(xml, xsd).each { println "Problem @ line $it.lineNumber, col $it.columnNumber : $it.message" } }
Recent Comments