Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Incremental XML parsing

by Corion (Pope)
on Feb 04, 2012 at 13:30 UTC ( #951799=note: print w/replies, xml ) Need Help??


in reply to Incremental XML parsing

XML::LibXML is a DOM parser and hence wants to read the whole document into memory before doing anything. XML::Parser is a SAX parser and can give you callbacks instead. You might want to look at XML::Twig, which attempts to give you the best of both worlds, giving you subtrees as soon as they become available.

Replies are listed 'Best First'.
Re^2: Incremental XML parsing
by choroba (Archbishop) on Feb 04, 2012 at 13:42 UTC
    You are right, but XML::LibXML::Reader is a pull parser, which means it does not load the entire file into memory. However, it can return the DOM object of any encountered node on request, which makes it more convenient than traditional SAX parsers.
Re^2: Incremental XML parsing
by Anonymous Monk on Feb 04, 2012 at 16:46 UTC
    In the XML::Twig docs, it says:
    WARNING: this option is NOT used when parsing with the non-blocking parser (parse_start, parse_more, parse_done methods) which you probably should not use with XML::Twig anyway as they are totally untested!

      XML::Twig can (and is actually designed to) parse big document. It doesn't do it by using an incremental parser, but by building a (non-DOM) tree for each element it parses. It lets you call handlers on elements, using the twig_handlers or twig_roots options. These handlers are called as soon as the element is completely parsed. In the handler you can process the element, then release the memory it used, by calling purge or flush (which also outputs the tree so far). This doesn't work when you need to have the entire tree available for processing, but in practice it does work in most cases, where you can treat the overall XML as a collection of independant elements. See the section Processing an XML document chunk by chunk in the docs.

      Using XML::Parser incremental parsing methods probably works in XML::Twig, I just never tested it, because that's not what XML::Twig needs. It does just fine with the regular interface, calling handlers during parsing.

      XML::LibXML offers a different interface to parse incrementally the XML, look into XML::LibXML::Reader.

        XML::LibXML offers a different interface to parse incrementally the XML, look into XML::LibXML::Reader.
        It's like nobody's reading the entire thread. I specifically asked about using XML::LibXML::Reader in my OP.
      Thatg warning is from the documentation of "keep_encoding". I don't understand how it applies to the problem of parsing a file withohut creating the complete DOM in memory. Can you please explain how it applies?
        Because it explicitly says that the non-blocking parser should not be used with XML::Twig and is untested. Yes, the author chose to include the only mention of the non-blocking parser in the documentation for an unrelated option, but the docs in general are quite confusing. As it stands, XML::LibXML::Reader has similar functionality to XML::Twig, but is much, much faster. But neither appear to be able to parse XML incrementally. Note this means not only to not produce the entire DOM at once, but also to be able to consume the input document in pieces, such as feeding data as it arrives over the wire.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://951799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-11-30 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?