Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

break out of SAX parsing

 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the advise, would the exception handling overhead actually cost me more than if I just use DOM?
thanks
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic