Re: Shortcutting a SAX parser

Emmanuel Rodriguez <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
On Tue, Mar 31, 2009 at 12:33 PM, Michael Ludwig <[email protected]> wrote:

> I have thousands of documents each of which contains some information
> in the attributes of the document element. I'm only interested in this
> particular information and not in the rest of the document. So I wrote a
> SAX parser to extract only the relevant information and not bother to
> examine the rest of the document.
>
> So this works, but still the documents are parsed in their entirety.
> This is not necessary - I know they're well-formed, and there is nothing
> down the pipe of interest here.
>
> 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:
>
>
Hi Michael,

There are many kinds of XML parsers: DOM (tree based and memory pig), SAX
(event based and uses less memory) and Pull parsers.
See
http://www.programmersheaven.com/user/pawanspace/blog/609-XML-parsers-Push-versus-Pull-parsers/

What you want is a Pull parser, it consists of an XML parser that will parse
the document only at you request. Once you find the element(s) you need you
can close the parser. There's no need to parse the whole file.

LibXML provides a pull parser (
http://search.cpan.org/perldoc?XML::LibXML::Reader). I've never used it but
I'm sure that others can help you with this.

-- 
Emmanuel Rodriguez

_______________________________________________
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.