如何在Java中有效地使用SAXParseException [英] How to use SAXParseException effectively in Java

查看:162
本文介绍了如何在Java中有效地使用SAXParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我将使用这些例外来突出显示验证失败的地方 - 但是SAXParseExceptions似乎有点太低了。

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?

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

Update @timgilbert, thanks for the response.

例如,我在t'internet上找到的SAXParseException

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})*'

对我来说关键的事情是


  • 此例外适用的元素'en'。为什么我不能调用exception.getElement()或者其他东西,为什么不对有问题的元素进行XPath?对于内存文档比行和列号更有用!

  • 这是模式验证失败。为什么我不能得到类似可能类型的失败的枚举和对相应失败的引用?

  • 验证失败的实际模式。

  • 接下来会抛出另一个异常告诉我'en'元素的值导致我需要合并的问题

  • 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 :)

我想我可能会错误地使用'reference-to-element'事情,也许我应该默认将文档的身份转换作为验证的一部分,并使用我可以用CSS选择的validation-error属性来扩充转换。如果我需要解析消息以使它们更友好,那仍然无济于事......

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:紧密绑定,javax.xml.validation.Validator。无论如何,validate()抛出org.xml.sax.SAXException - 不知道如何摆脱绑定......

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...

干杯

推荐答案

Brabster我也面临类似的问题,我需要告诉xml中哪个元素出错。我通过在SAX Parser的处理程序中维护堆栈来解决了这个问题。在startElement方法中,我在堆栈中推送qName(元素的名称),在endElement方法中,我从堆栈中弹出qName。

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.

每当异常来临时stack表示元素的完整XPath。

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

唯一的问题是如果有多个具有相同名称的元素,那么您不知道错误是针对哪个元素。但至少完整的XPath细节与LineNumber和ColumnNumber有关。

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.

希望这会有所帮助。

这篇关于如何在Java中有效地使用SAXParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆