473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reporting line number of parse exception?

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
4 1766


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


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
Thanks Martin, that did the trick.

-Rolf

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: steve | last post by:
Hi, In my script (phpnuke), whenever there is access to database, there is this line of code: message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__, $sql); Is there a more...
4
by: Liang | last post by:
Hi all, I want to diff two files or two versions of one file, and parse the output to find a summary of how many lines of replacement/addition/deletion in the two files. Known from...
2
by: RdS | last post by:
Hello, I use sourcesafe and vb 2003 for my dev environment. on the sourcesafe server I also have sql2005 and reporting services installed. The web app references this sql server for db. When...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
4
by: Ravi, Dallas, Texas | last post by:
http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic22503.aspx shows ways to get errors in a server context. i am trying to find ways to trap the line number where...
1
by: sophie_newbie | last post by:
I'm sure this is exceedingly simple but I can't find it anywhere. When I catch an exception I would like to report the line number of the exception as well as the error info. try: someError()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.