Validating a XML against a XSD schema and catching validator expection with groovy

hellrider

New Member
\[code\]def validateXml(xml){ String xsd = "src/main/ressources/fulltext-documents-v1.2.3.xsd" def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) def schema = factory.newSchema(new StreamSource(new File(xsd))) def validator = schema.newValidator() validator.validate(new StreamSource(new StringReader(xml)))}\[/code\]This is my function to validate a String representation of a xml document.Below an other function that catches the exceptions that could be raised by the validator\[code\]def xmlVerification(xml) { Node rootNode = new XmlParser().parseText(xml) def stringXml = XmlUtil.serialize(rootNode) try{ validateXml(stringXml) println "no error in text" }catch(SAXParseException e){ println "column number "+e.getColumnNumber() println "line number"+e.getLineNumber() }}\[/code\]For now it only shows the column and line number where the exception was raised (Good enough for me at the moment).Now, lets assume that I have a document with at least 2 errors. What I wish is to get these 2 errors (in a table for example) and then treat them.With my code, it stops on the first exception raised, so I can't handle the 2 errors. I have to correct the first one in order to correct the second one (by re-running my code a second time).Any idea how I can go through the entire document, stock all the exceptions and treat them in a .each{} loop or something like that ?Hope it was clear enoughThanks in advance !
 
Back
Top