在标准Java环境中针对xsd模式验证xml源时,我无法找到获取有关未通过验证的元素(在许多特定情况下)的信息的方法。如何在SAXParseException中获取XML元素信息
当捕获一个SAXParseException时,元素的信息消失了。但是,在调试到xerces.XmlSchemaValidator时,我可以看到原因是没有定义特定的错误消息来提供有关该元素的信息。
例如(我的java演示中也是这种情况)“cvc -mininclusive-valid”错误是这样定义的: cvc-minInclusive-valid:值''{0}''不是facet对于类型“{2}”,minInclusive''{1}''有效。 https://wiki.xmldation.com/Support/Validator/cvc-mininclusive-valid
什么我宁愿是,这样的信息将被产生: CVC-type.3.1.3:该值“” {1}“”元素“的” {0}“”是无效。 https://wiki.xmldation.com/Support/Validator/cvc-type-3-1-3
当调试到xerces.XMLSchemaValidator时,我可以看到有两个连续的调用reportSchemaError(...) - 第二个只发生,如果第一个没有引发异常返回。
有什么办法可以配置验证器使用第二种报告方式或用元素信息丰富SAXParseException吗?
请参阅我复制&粘贴&可运行下面的例子代码作进一步的解释:
String xsd =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\">" +
"<xs:element name=\"demo\">" +
"<xs:complexType>" +
"<xs:sequence>" +
// given are two elements that cannot be < 1
"<xs:element name=\"foo\" type=\"xs:positiveInteger\" minOccurs=\"0\" maxOccurs=\"unbounded\" />" +
"<xs:element name=\"bar\" type=\"xs:positiveInteger\" minOccurs=\"0\" maxOccurs=\"unbounded\" />" +
"</xs:sequence>" +
"</xs:complexType>" +
"</xs:element>" +
"</xs:schema>";
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<demo>" +
"<foo>1</foo>" +
// invalid!
"<foo>0</foo>" +
"<bar>2</bar>" +
"</demo>";
Validator validator = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(new StreamSource(new StringReader(xsd)))
.newValidator();
try {
validator.validate(new StreamSource(new StringReader(xml)));
} catch (SAXParseException e) {
// unfortunately no element or line/column info:
System.err.println(e.getMessage());
// better, but still no element info:
System.err.println(String.format("Line %s - Column %s - %s",
e.getLineNumber(),
e.getColumnNumber(),
e.getMessage()));
}
不幸的是,我不认为这是可能的。如果您必须具有元素名称,则可以编写自己的代码以根据行号和列号找到元素名称,但即使如此,我也不认为这些元素始终保证可靠。 – Matthew
感谢您的评论,@Matthew!我会给我的问题多一点时间,直到我失去希望,你是对的:) – realsim