翻译 SAX 异常

发布于 2024-10-05 00:59:57 字数 1478 浏览 3 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2024-10-12 00:59:57

好吧,看来我必须将 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 是必要的:

xmlDataStr = xmlDataStr.replace("<rootNode ", "<rootNode xsi:schemaLocation=\"http://www.mycompany.com/schema resources/schema1.xsd \" ");

...当然现在我遇到了双重验证错误!一个清晰易懂的错误,例如:

cvc-complex-type.2.4.a: Invalid content was found starting with element 's:cID'. One of '{"http://www.mycompany.ca/schema":tdr}' is expected.

紧接着:

http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:cID&{"http://www.mycompany.com/schema":tdr}

双重错误很烦人,但至少第一个错误是可用的......

Well, it seems I had to add xsi:schemaLocation="http://www.mycompany.com/schema resources/schema1.xsd " to the XML document, because s: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:

xmlDataStr = xmlDataStr.replace("<rootNode ", "<rootNode xsi:schemaLocation=\"http://www.mycompany.com/schema resources/schema1.xsd \" ");

...of course now I'm getting double validation errors! A clear and intelligible one such as:

cvc-complex-type.2.4.a: Invalid content was found starting with element 's:cID'. One of '{"http://www.mycompany.ca/schema":tdr}' is expected.

Immediately followed by:

http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?s:cID&{"http://www.mycompany.com/schema":tdr}

The double-error is annoying but at least the first one is usable...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文