Re: Shortcutting a SAX parser

Robin Berjon <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Hi Michael,

On Mar 31, 2009, at 06:33 , Michael Ludwig wrote:
> It occurred to me that I could shortcut the process by throwing an
> exception to terminate the parser once the data is harvested, catch  
> the
> exception, and then move on to the next document. This is just a  
> trivial
> change to the code:
>
> (1) die in start_element after having collected the data
> (2) protect the parser invocation: eval { $parser->parse_uri( $_) };
>
> As the data I need is at the very start of the document, this is
> significantly faster, even though the documents are only 35 KB on
> average.
>
> Is dying the way to go here? Or is there a more elegant and  
> recommended
> way to terminate the parser? Other thoughts or comments?

Yes, you guessed right (in fact it's documented in http://perl-xml.sourceforge.net/perl-sax/ 
  but I can't get you the exact pointer as my hotel connection is  
horridly slow), that's a perfectly valid approach.

If you want to be absolutely strict and defensive in your programming,  
you probably want to throw a specific exception (SAX exceptions are  
expected to have at least a Message key with a reason for throwing)  
and check that that's the one you're getting. That way, if your  
document does turn out to have a problem, your code won't confuse  
"threw because I got what I wanted" with "threw because something  
smells bad".

-- 
Robin Berjon - http://berjon.com/
     Feel like hiring me? Go to http://robineko.com/

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.