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 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/
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
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/
Thanks Martin, that did the trick.
-Rolf This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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?
|
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...
|
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")
|
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...
| |
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...
|
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?
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |