java - 如何从 SAX-Parser 获取所有验证错误?

标签 java xml validation groovy saxparser

我想从 SAX-Parser 获取所有验证错误,但对于我的代码片段,我只收到第一个。我怎样才能实现这个目标?

谢谢!

片段

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def xml = new StreamSource(inputStream)
def xsd = new StreamSource(new FileReader(schema), systemId)

try {
    factory?.newSchema(xsd)?.newValidator()?.validate(xml)
} catch(SAXParseException saxpe) {
    continueImport = false
    log.error("Error while parsing the import xml", saxpe)
}

最佳答案

您是否尝试过实现 ErrorHandler ?这将使您接收并记录错误/警告和 fatal error 。

来自文档:

If a SAX application needs to implement customized error handling, it must implement this interface and then register an instance with the XML reader using the setErrorHandler method. The parser will then report all errors and warnings through this interface.

这是一个code example .

关于java - 如何从 SAX-Parser 获取所有验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887680/

相关文章:

java - Connection 对象可以在构造函数之外与 JDBC RowSet 实例一起使用吗?

java - 如何让 HttpUrlConnection 返回 JSON 格式的数据?

java - 如何在Android中输入数字和算术字符

php - CodeIgniter 输入和验证

html - 在基于十进制的输入字段上设置小于数字的最大值

三层架构中的验证

java - Android 动画无法正常工作

java - Spring MVC Controller 配置 - 混合注释和 XML @PathVariable

java - 某些 mipmap 不显示

xml - 如何从应用程序描述 rune 件中读取版本