473,586 Members | 2,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
SAXParseExcepti on 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:

SAXParserFactor y factory = SAXParserFactor y.newInstance() ;
SAXParser parser = factory.newSAXP arser();
InputStream in = new FileInputStream (filename);
MyHandler handler = new MyHandler();
InputSource source = new InputSource(in) ;
parser.parse(so urce, handler);

-Rolf

Jul 20 '05 #1
4 1783


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
SAXParseExcepti on 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:

SAXParserFactor y factory = SAXParserFactor y.newInstance() ;
SAXParser parser = factory.newSAXP arser();
InputStream in = new FileInputStream (filename);
MyHandler handler = new MyHandler();
InputSource source = new InputSource(in) ;
parser.parse(so urce, handler);


As far as I understand it your handler needs to implement the method
setDocumentLoca tor
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
SAXParseExcepti on 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
setDocumentLoca tor
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
setDocumentLoca tor? javax.xml.parse rs.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
setDocumentLoca tor
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
setDocumentLoca tor? javax.xml.parse rs.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
2041
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 elegant way of reporting line number besides putting this line everywhere I access db. I want to just write a function, which also globally knows about...
4
4939
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 diff/cleardiff, the output has a style like: 15a16, 15,17d3, 18c19,21 etc. Anyone know how to parse these output to generate a summary?
2
5314
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 I compile the app on my local machine which is used for development I am able to lauch application with no problem. When I run my web app on my...
4
2998
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") _prtSKU = myDLReader.GetString("prtSKU") _prtPic = myDLReader.GetString("prtPic") _prtRsvQty = myDLReader.GetInteger("prtRsvQty")
19
107915
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 sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular...
4
1416
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 the code is failing in try catch set up. if i hard code the line number by looking at the IDE's editor, it will be soon out of date when i start...
1
15689
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() except Exception, e: "print_error_and_line_number" How do I find the line number?
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.