title: Validating a XML against a XSD schema and catching validator expection with groovy
body: <pre><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></pre>
  
  <p>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</p>
  
  <pre><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></pre>
  
  <p>For now it only shows the column and line number where the exception was raised (Good enough for me at the moment).</p>
  
  <p>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).</p>
  
  <p>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 ?</p>
  
  <p>Hope it was clear enough</p>
  
  <p>Thanks in advance !</p>