如何在 Java 中有效使用 SAXParseException

发布于 2024-07-16 02:29:07 字数 1123 浏览 5 评论 0原文

我正在 Java 中针对 XMLSchema 进行验证,并且当我有无效的内容模型时抛出 SAXParseExceptions。

我将使用这些异常来突出显示验证失败的位置 - 但 SAXParseExceptions 似乎有点太低级了。

例如,对于枚举失败,我得到了有效性错误,即所提供的值与一个异常中的内容模型不匹配,以及它在下一个异常中应用的元素。

我想我需要一个实用程序来抽象一些将相关错误合并在一起并将异常文本解析为可用的异常属性。

这是一个合理的方法,还是我只是错过了一些东西,或者可能是一个库或辅助类?


更新@timgilbert,感谢您的回复。

例如,我在 t'internet 上发现的 SAXParseException

cvc-pattern-valid: Value 'en' is not facet-valid
with respect to pattern '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*'

对我来说关键是

  • 该异常适用的元素 'en'。 为什么我不能调用 exception.getElement() 或其他东西,为什么不能提供相关元素的 XPath? 对于内存中的文档来说,比行号和列号更有用!
  • 这是模式验证失败。 为什么我无法获得可能的故障类型的枚举以及对相应故障类型的引用之类的信息?
  • 验证失败的实际模式。
  • 接下来会抛出另一个异常,告诉我导致我需要合并的问题的“en”元素的值我想要

做的一个例子是让人们提交一个文档并拥有文档突出显示验证失败并显示用户友好的消息 - 上面的错误消息似乎不太友好...必须用单引号解析感觉就像等待发生的事故:)

我想我可能做错了有了“引用元素”的东西,也许我应该默认对文档进行身份转换作为验证的一部分,并使用我可以用 CSS 挑选出来的验证错误属性来增强转换。 如果我需要解析消息以使它们更友好,那仍然无济于事...

回复:紧密绑定,javax.xml.validation.Validator.validate() 无论如何都会抛出 org.xml.sax.SAXException - 不确定我怎样才能摆脱假设的约束......

干杯

I'm validating against XMLSchema in Java, and getting SAXParseExceptions thrown when I have non-valid content models.

I'm going to be using these exceptions to highlight where the validation has failed - but the SAXParseExceptions seem to be a little too low-level.

For example, for a failure on an enumeration, I get the validity error that the value provided doesn't match the content model in one exception, and the element it applies to in the next.

I'm thinking I need to have a utility that abstracts a little to merge related errors together and parse exception text into useable exception properties.

Is this a reasonable approach, or am I just missing something, or maybe a library or helper class?


Update @timgilbert, thanks for the response.

For example, a SAXParseException I found on t'internet

cvc-pattern-valid: Value 'en' is not facet-valid
with respect to pattern '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*'

The key things for me are

  • Element 'en' to which this exception applies. Why can I not call exception.getElement() or something, and why not an XPath to the element in question? More useful with an in-memory document than the line and column number!
  • It's a pattern validation failure. Why can I not get something like a enumeration of possible types of failure and a reference to the appropriate one?
  • The actual pattern that validation has failed against.
  • There'll be another exception thrown next to tell me the value of the 'en' element that caused the problem that I need to merge

An example of what I'd like to be able to do is have people submit a document and have the document highlighted where validation fails with a user friendly message - the error message above kinda doesn't seem very friendly... having to parse by single quotes just feels like an accident waiting to happen :)

I think I'm maybe doing it wrong with the 'reference-to-element' thing, and perhaps I should have an identity transform of the document by default as part of the validation, and augment the transformation with validation-error attributes that I can pick out with CSS. That still won't help if I need to parse the messages to make them more friendly though...

Re: tight binding, javax.xml.validation.Validator.validate() throws org.xml.sax.SAXException anyway - not sure how I can get away from assuming the binding...

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

白馒头 2024-07-23 02:29:07

Brabster 我也遇到过类似的问题,我需要告诉 xml 中的哪个元素出现了错误。 我通过在 SAX 解析器处理程序中维护一个堆栈稍微解决了这个问题。 在 startElement 方法中,我将 qName(元素的名称)压入堆栈中,在 endElement 方法中,我从堆栈中弹出 qName。

每当出现异常时,我的堆栈就代表元素的完整 XPath。

唯一的问题是,如果有多个同名元素,那么您不知道错误是针对哪个元素的。 但至少完整的 XPath 详细信息以及 LineNumber 和 ColumnNumber 都有帮助。

希望这可以帮助。

Brabster I too faced the similar issue way back where I need to tell for which element in the xml the error has come. I solved the problem little bit by maintaining a stack in my handler of SAX Parser. In the startElement method I push the qName (name of the element) in the stack and in the endElement method I pop the qName from the stack.

Whenever an exception comes my stack represents the full XPath of the element.

The only problem was if there are multiple elements with the same name then you don't know the error is for which element. But at least the full XPath details helped along with the LineNumber and ColumnNumber.

Hope this helps.

〆凄凉。 2024-07-23 02:29:07

我不完全清楚你在这里问的是什么,也许你可以提供更多关于异常太低级别的含义的细节? 是不是错误信息本身就难以理解?

SaxParseException 类确实具有 getColumnNumber() 和 getLineNumber() 方法,您可以将它们提供给用户以让他们修复错误。

您可能会尝试的一件事是使用不同的 XML 解析实现 - 每个解析器在发现无效代码时都会抛出错误,但不同的实现可能有不同的错误消息和异常链。

实际上,出于这个原因,我对尝试构建一个检查异常链并尝试从中构建更连贯的错误消息的库持怀疑态度,因为您会将代码与异常的具体细节紧密耦合。 XML 解析实现(特别是如果您依赖于错误消息的特定措辞)。

(抱歉,这不是更具体,也许你可以举一个你所看到的问题的例子?)

I'm not entirely clear about what you're asking here, maybe you could provide a little more detail about what you mean by the exceptions being too low-level? Is it that the error messages themselves are not comprehensible?

The SaxParseException class does have getColumnNumber() and getLineNumber() methods which you can present to the user to get them to fix the errors.

One thing that you might experiment with is using different XML-parsing implementations - every parser will throw an error when it finds invalid code, but different implementations may have different error messages and exception chains.

Actually, for this reason I'd be dubious about trying to build a library which inspects the exception chain and tries to build a more coherent error message out of it, since you'd be coupling your code fairly tightly to the specific details of an XML-parsing implementation (especially if you're relying on the specific verbiage of error messages).

(Sorry this isn't more specific, maybe you could give an example of the problem you're seeing?)

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