如何根据 XSD 模式验证 XML 文件并列出所有验证错误
How to validate XML file against XSD schema and list all validation errors
我正在针对 xsd 验证 xml,当遇到第一个验证错误时会抛出第一个异常,但是,通过这种方法,我们无法获得有关 [=16] 中所有验证错误的信息=] 文件中的单个 运行。如果一个 XML 文件有多个验证错误,那么在第一个 运行 中,一旦遇到第一个错误就会抛出异常,而我们不知道剩余的错误。要了解后续错误,我们需要修复之前的错误并反复验证文件,直到没有抛出异常。
onException( SchemaValidationException.class )
.handled(true)
.to("file:invalid-data")
from("file:in-data?noop=true")
.to("validator:file:my.xsd")
.to("file:out-data");
您使用的是哪个 Apache Camel 版本?在 2.20 中,validation code 会在验证后处理所有错误:
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
验证是由 javax.xml.validation.Validator
class 执行的。请参阅有类似讨论的 this question。文档指出:
Errors found during the validation is sent to the specified ErrorHandler.
If a document is valid, or if a document contains some errors but none of them were fatal and the ErrorHandler didn't throw any exception, then the method returns normally.
也许您面临的错误是致命的?如果是这样,我认为这超出了 Camel 的组件控制范围。 :(
我正在针对 xsd 验证 xml,当遇到第一个验证错误时会抛出第一个异常,但是,通过这种方法,我们无法获得有关 [=16] 中所有验证错误的信息=] 文件中的单个 运行。如果一个 XML 文件有多个验证错误,那么在第一个 运行 中,一旦遇到第一个错误就会抛出异常,而我们不知道剩余的错误。要了解后续错误,我们需要修复之前的错误并反复验证文件,直到没有抛出异常。
onException( SchemaValidationException.class )
.handled(true)
.to("file:invalid-data")
from("file:in-data?noop=true")
.to("validator:file:my.xsd")
.to("file:out-data");
您使用的是哪个 Apache Camel 版本?在 2.20 中,validation code 会在验证后处理所有错误:
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
验证是由 javax.xml.validation.Validator
class 执行的。请参阅有类似讨论的 this question。文档指出:
Errors found during the validation is sent to the specified ErrorHandler. If a document is valid, or if a document contains some errors but none of them were fatal and the ErrorHandler didn't throw any exception, then the method returns normally.
也许您面临的错误是致命的?如果是这样,我认为这超出了 Camel 的组件控制范围。 :(