in reply to Re^3: Memory Efficient XML Parser
in thread Memory Efficient XML Parser
The data structure built by XML::LibXML is much bigger because it contains much more data. Most of that of no use whatsoever, but still present. It remembers whether the name or city was first, how much whitespace there was around them, that the John Smith and London was the content instead of an attribute etc. Please reread what I said ... you do NOT have to build such a structure. And even if you do build a structure first you can build a specialized (containing only what you need and in a convenient format) data structure. In this particular case this would build a structure equivalent to the one created by Storable using XML::Rules:
As the transformations are done during the parsing you end up using just a little more memory than the Storable solution. Though it will of course be somewhat slower. Everything comes at a price, even generality. (The version of XML::Rules that supports stripspaces will be released this weekend, the currently released version would actually keep on eating memory because it would keep the whitespace between the <address> tags until the XML is fully processed. I'll update this node once it's released. Sorry.)use XML::Rules; my $parser = XML::Rules->new( stripspaces => 3, rules => [ _default => 'content', address => 'no content array', addresses => 'pass no content', ] ); my $data = $parser->parse($XML); use Data::Dumper; print Dumper($data->{address});
Unlike Storable you can process the XML in chunks:
my $parser = XML::Rules->new( stripspaces => 3, rules => [ _default => 'content', address => sub {print "$_[1]->{name} for $_[1]->{city}\n"; ret +urn}, addresses => '', ] ); $parser->parse($XML);
Of course if you need to store something and the read it completely both using a Perl script then Storable is a better solution, but if you need to exchange data with other systems, XML is most likely the way to go. Whether you waste memory and CPU while processing it or not is up to you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Memory Efficient XML Parser
by eserte (Deacon) on Dec 14, 2007 at 20:57 UTC |