翻译 SAX 异常
我有一些 Java 代码可以根据 XSD 验证 XML。我正在使用此处找到的错误处理程序的修改版本: http:// www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html 用于在验证时捕获并记录所有异常。
这些错误非常简洁,它们看起来像这样:
http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:cID&{"http://www.myschema.com/schema":txn}
其他消息
http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:attributes&{"http://www.myschema.com/schema":sequence}
甚至更加神秘。
有没有一种简单的方法可以从 SAX 中获取清晰易懂的消息来解释这里出了什么问题?我认为在第一个错误中,它期望的是 txn
,但却找到了元素 cID
。但是...我不知道 SAX 可能生成的所有可能的错误,所以我不想尝试手动创建转换表。
此输出的最终用户大多是非技术人员,因此我需要能够生成简单明了的消息,例如“element txn
was out of order”。
如果有帮助,这里是用于验证的代码(或多或少):
Source schema1 = new StreamSource(new File("resources/schema1.xsd"));
Source schema2 = new StreamSource(new File("resources/schema2.xsd"));
Source[] sources = {schema1,schema2};
validator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(sources).newValidator();
ErrorHandler lenient = new ForgivingErrorHandler();
validator.setErrorHandler(lenient);
其他地方......
StreamSource xmlSource = new StreamSource(new StringReader(XMLData) );
try
{
validator.validate(xmlSource);
}
catch (SAXException e)
{
logger.error("XML Validation Error: ",e);
}
I have some Java code that validates XML against an XSD. I am using a modified version of the Error Handler found here: http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html to catch and log ALL exceptions while validating.
The errors are very terse, they look something like this:
http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:cID&{"http://www.myschema.com/schema":txn}
Other messages such as
http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:attributes&{"http://www.myschema.com/schema":sequence}
are even more cryptic.
Is there an easy way to get a clear and intelligible message out of SAX explaining what went wrong here? I think in the first error it was expecting txn
and instead found the element cID
. BUT... I don't know all the possible errors that might be generated by SAX so I'd rather not try to manually create a translation table.
The eventual users of this output are mostly non-technical so I need to be able generate simple and clear messages such as "element txn
was out of sequence".
If it helps, here's the code (more or less) that's used for validation:
Source schema1 = new StreamSource(new File("resources/schema1.xsd"));
Source schema2 = new StreamSource(new File("resources/schema2.xsd"));
Source[] sources = {schema1,schema2};
validator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(sources).newValidator();
ErrorHandler lenient = new ForgivingErrorHandler();
validator.setErrorHandler(lenient);
Elsewhere...
StreamSource xmlSource = new StreamSource(new StringReader(XMLData) );
try
{
validator.validate(xmlSource);
}
catch (SAXException e)
{
logger.error("XML Validation Error: ",e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看来我必须将
xsi:schemaLocation="http://www.mycompany.com/schema resources/schema1.xsd "
添加到 XML 文档中,因为s:http: //www.mycompany.com/schema
是默认命名空间:xmlns="s:http://www.mycompany.com/schema"
。当然,我无权修改生成 XML 的工具,因此以下丑陋的 hack 是必要的:...当然现在我遇到了双重验证错误!一个清晰易懂的错误,例如:
紧接着:
双重错误很烦人,但至少第一个错误是可用的......
Well, it seems I had to add
xsi:schemaLocation="http://www.mycompany.com/schema resources/schema1.xsd "
to the XML document, becauses:http://www.mycompany.com/schema
is the default namespace:xmlns="s:http://www.mycompany.com/schema"
. Of course, I don't have access to modify the tool that generates the XML, so the following ugly hack was necessary:...of course now I'm getting double validation errors! A clear and intelligible one such as:
Immediately followed by:
The double-error is annoying but at least the first one is usable...