How to use SAXParseException effectively in Java

一曲冷凌霜 提交于 2019-12-02 23:11:38

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?)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!