aspose file tools*
The moose likes XML and Related Technologies and the fly likes break out of SAX parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "break out of SAX parsing " Watch "break out of SAX parsing " New topic
Author

break out of SAX parsing

jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
dear guru,
I am comparing the uses of DOM and SAX for my application. Sometimes I wish to check a single attribute within an element and do nothing more than that. If I use DOM I need to read the whole xml document into memory. So I am thinking about using SAX. When parsing a document with the DefaultHandler, I wish to quit parsing as soon as I find what I am looking for. Is this possible?
for exmaple the following method in ContentHandler()
startElement(String uri, String localName, String qName, Attributes attributes);
Is it possible to break out of parsing from within this callback method?
If this is not possible, I might as well just use dom instead of sax because performance-wise both of them will parse through the whole document, the only difference is that dom will create new node objects which might decrease performance.
So should I choose SAX over DOM for my purpose?
thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Just throw an Exception when you want to stop. You could even make a custom Exception class to contain the data you are trying to locate.
Bill
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
thanks for the advise, would the exception handling overhead actually cost me more than if I just use DOM?
thanks
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
If I throw an exception in the callback method, how do I catch it from its outter class? for example, in the following code, I can't catch the exception from the parser.parse level.
thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Of course you can catch the Exception.
If there is a try{}catch{} where the try encloses the parse call, you WILL get a chance at the exception in the catch.
just catch( MessageWrapper myW )
as the first catch - you might also catch IOExeception, SAXParseException etc etc. in a typical chain of catch statements.
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
thanks but no, you can not catch the exception from a method in the anonymous enclosing inner class..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Have you actually tried this or what?
Think - what thread is executing the parse method?
What thread will the exception occur in?
Try it and get back to me.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: break out of SAX parsing