Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to extract some meaningful information from a SAXParseException?

Status
Not open for further replies.

Nilesh002

Programmer
Jun 26, 2003
1
IN
Hi,

I am using Xerces 2.4.0 to validate my xml documents.
The code is the usual parser stuff like
-----------
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setAttribute(" "dbf.setAttribute(.....) etc etc.
----------

My schema has a lot of rules with many restrictions on several nodes.

Whenever I give an invalid xml file (non conforming to my schema) to the parser, it throws SAXParseException.
These exceptions are logged in my logs.

For example,
org.xml.sax.SAXParseException: cvc-minInclusive-valid: Value '-240' is not facet-valid with respect to minInclusive '1'.
org.xml.sax.SAXParseException: cvc-minInclusive-valid: Value '-400' is not facet-valid with respect to minInclusive '1'.
org.xml.sax.SAXParseException: cvc-maxInclusive-valid: Value '40000000' is not facet-valid with respect to maxInclusive '1500'.


As you can see the error messages are not much useful. I want to get the name of the node in the xml file which caused
the problem. If I can get the name of the node/element in the xml file, I will be able to display more meaningful
error messages to the user such as:

"The value entered for cost is invalid: -240". etc.

Is there any way I can extract more information out of the SAXParseException,
so that I can get the node's name and/or value?


Thanks in advance.
 
Try using an instance of the 'SAXParser' class and then create a class that extends 'DefaultHandler' that you set as the content handler and error handler.

'DefaultHandler' implements the 'ContentHandler' and 'ErrorHandler' interfaces. You can save the name of the current tag using the ContentHandler's 'startElement()' method. The ErrorHandler interfaces has 'error()', 'fatalError()' and 'warning()' methods to get error and warning messages.

'SAXParseException' has these methods that may be of use to you:
- getColumnNumber()
- getLineNumber()

The 'SAXException' superclass of 'SAXParseException' has these methods that also may be of use to you:
- getException()
- getMessage()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top