File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes SAXException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "SAXException" Watch "SAXException" New topic


Kutts Kutts

Joined: May 27, 2004
Posts: 1
I am a beginner in SAX. I am able to get the error message on the vent of an exception (error or warning). But I want to get the element which caused it. How can that be done?. eg: The error msg shown now is like " Attribute "RollNumber" is required and must be specified for element
"StudentDetails". in entity file:///D:/abc/def/classes/abc.xml"

What I want is a method by which I get the element name only ie StudentDetails and the value which is wrong(in case it is a Data Value error).

Whatever happends is always for the best :-)
Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
Welcome to javaranch, please change your display name according to the policy.
From the error message, it implies to me that your parser has the schema validation turned on.
Please chekc whether your xml is valid to your xmlschema first.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
If you want the element that caused the parsing exception, I see two options:
1) Use SAXParseException#getLineNumber() and SAXParseException#getColumnNumber() to find out the location of the error and seek out that particular point in the XML file
2) In your handler class, keep count of which element you're "in" at any one time. Then, when you get an exception from parse(), ask the handler object for the name of the last element it entered successfully.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: SAXException
It's not a secret anymore!