Re: parsing a huge xml document
Petr Pajas <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
2009/9/11 Petr Cimprich <[email protected]>: > Liam Quin wrote: >> >> Interesting! >> >> Others have mentioned that SAX is slow (although I think one of >> SAX readers is based on libxml and shouldbe quite fast), > > In fact, one parsers based on Expat is any faster (ExpatXS). > >> and >> that the documentatoin is needlessly difficult and obscure, which >> I'd 100% agree with. > > Here is the Perl SAX docs: > http://perl-xml.sourceforge.net/perl-sax/sax-2.1-ref.html > > If you can refer to the most needlessly difficult and obscure parts I'm > happy to change them :) > > When memory is not a constrain the DOM or even XSLT is faster than Perl > SAX (C vs. Perl land). If the memory matters and the processing is > fairly simple Perl SAX can be the right tool. > > Petr Liam, if performance is your main interest, then you can also use the pull parser, XML::LibXML::Reader, which has just a small, constant memory usage. Typically, it can be faster than SAX because it generates less amount of subroutine calls, especially the expensive XS calls (Perl<->C round trips). You can even let the parser skip uninteresting parts of the document. Moreover, you can use it in combination with XML::CompactTree::XS, which reads the element at the current reader position (including its children and descendants) or even a sequence of sibling elements into Perl data structures in just one XS call; since traversing Perl data structures is multiple times faster than traversing DOM implemented in C or gathering data via many SAX callbacks, you can really boost the parsing speed with this approach, reading the document chunk by chunk. The drawback is that the API is not as convenient as that of some other modules. -- Petr P. _______________________________________________ Perl-XML mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs