Reporting line number of parse exception?

R

rolf

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
 
M

Martin Honnen

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()
 
R

Rolf Howarth

Martin said:
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
 
M

Martin Honnen

Rolf Howarth wrote:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top