By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Reporting line number of parse exception?

P: n/a
Hmm, I seem to be missing something basic with regards to SAXParsers.

I'm parsing some XML using Java 1.4.2 and SAX, so I extend
DefaultHandler and in my startElement() method I detect some kind of
error, maybe a missing mandatory attribute. So I throw an exception,
and of course I want to report the location (line number and character)
within the XML file that the error occurred in. I could throw a
SAXParseException that has this information, but... where do I get the
location from? It seems likke I could do with a Locator, but
DefaultHandler only has a setLocator(), no getLocator(). Surely the
parser or the InputSource knows where it's got to in the file when an
error occurs - is there no way of getting hold of this information?

I'm constructing the parser as follows:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputStream in = new FileInputStream(filename);
MyHandler handler = new MyHandler();
InputSource source = new InputSource(in);
parser.parse(source, handler);

-Rolf

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


ro**@squarebox.co.uk wrote:
Hmm, I seem to be missing something basic with regards to SAXParsers.

I'm parsing some XML using Java 1.4.2 and SAX, so I extend
DefaultHandler and in my startElement() method I detect some kind of
error, maybe a missing mandatory attribute. So I throw an exception,
and of course I want to report the location (line number and character)
within the XML file that the error occurred in. I could throw a
SAXParseException that has this information, but... where do I get the
location from? It seems likke I could do with a Locator, but
DefaultHandler only has a setLocator(), no getLocator(). Surely the
parser or the InputSource knows where it's got to in the file when an
error occurs - is there no way of getting hold of this information?

I'm constructing the parser as follows:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputStream in = new FileInputStream(filename);
MyHandler handler = new MyHandler();
InputSource source = new InputSource(in);
parser.parse(source, handler);


As far as I understand it your handler needs to implement the method
setDocumentLocator
and if the SAX parser provides location information then it will call
that method and you can then process and store the Locator passed in and
access it when needed to call methods like
getLineNumber()
getColumnNumber()
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2

P: n/a
Martin Honnen wrote:
I'm parsing some XML using Java 1.4.2 and SAX, so I extend
DefaultHandler and in my startElement() method I detect some kind of
error, maybe a missing mandatory attribute. So I throw an exception,
and of course I want to report the location (line number and character)
within the XML file that the error occurred in. I could throw a
SAXParseException that has this information, but... where do I get the
location from? It seems likke I could do with a Locator, but
DefaultHandler only has a setLocator(), no getLocator(). Surely the
parser or the InputSource knows where it's got to in the file when an
error occurs - is there no way of getting hold of this information?


As far as I understand it your handler needs to implement the method
setDocumentLocator
and if the SAX parser provides location information then it will call
that method and you can then process and store the Locator passed in and
access it when needed to call methods like
getLineNumber()
getColumnNumber()


Right, but where does my handler get the Locator from to pass to
setDocumentLocator? javax.xml.parsers.SAXParser doesn't have any
methods to give me the information I need to implement Locator.

-Rolf

Jul 20 '05 #3

P: n/a


Rolf Howarth wrote:

As far as I understand it your handler needs to implement the method
setDocumentLocator
and if the SAX parser provides location information then it will call
that method and you can then process and store the Locator passed in and
access it when needed to call methods like
getLineNumber()
getColumnNumber()

Right, but where does my handler get the Locator from to pass to
setDocumentLocator? javax.xml.parsers.SAXParser doesn't have any
methods to give me the information I need to implement Locator.


You need to implement the interface in your handler and the SAX parser
then calls the method you provide and passes the Locator in.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #4

P: n/a
Thanks Martin, that did the trick.

-Rolf

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.