2
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)))
}

这是我验证 xml 文档的字符串表示形式的函数。在另一个捕获验证器可能引发的异常的函数下方

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()
    }
}

现在它只显示引发异常的列和行号(目前对我来说已经足够了)。

现在,假设我有一个至少有 2 个错误的文档。我希望得到这两个错误(例如在表格中)然后处理它们。使用我的代码,它会在引发的第一个异常时停止,因此我无法处理这 2 个错误。我必须更正第一个才能更正第二个(通过第二次重新运行我的代码)。

知道如何浏览整个文档,存储所有异常并在 .each{} 循环或类似的东西中处理它们吗?

希望它足够清楚

提前致谢 !

4

1 回答 1

6

这应该做你想要的:

import org.xml.sax.ErrorHandler
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.Schema
import javax.xml.validation.SchemaFactory
import javax.xml.validation.Validator

List findProblems( File xml, File xsd ) {
  SchemaFactory factory = SchemaFactory.newInstance( W3C_XML_SCHEMA_NS_URI )
  Schema schema = factory.newSchema( new StreamSource( xsd ) )
  Validator validator = schema.newValidator()
  List exceptions = []
  Closure<Void> handler = { exception -> exceptions << exception }
  validator.errorHandler = [ warning:    handler,
                             fatalError: handler,
                             error:      handler ] as ErrorHandler
  validator.validate( new StreamSource( xml ) )
  exceptions
}

// Two files I got for testing
File xml = new File( 'books.xml' )
File xsd = new File( 'books.xsd' )

// Call the method, and print out each exception
findProblems( xml, xsd ).each {
  println "Problem @ line $it.lineNumber, col $it.columnNumber : $it.message"
}

或者一个更符合概念的 groovy 版本是:

import org.xml.sax.ErrorHandler
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

List findProblems( File xml, File xsd ) {
  SchemaFactory.newInstance( 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
  }
}
于 2013-03-27T10:07:12.103 回答