Hi CSharma,
but script takes a lot time in providing the output
How long does it take to gunzip the file and then process it with your existing script? How much longer does the above code take? To get a somewhat decent comparison, try piping the output of gunzip into your script (it'll need a slight modification to read from STDIN).
One thing that might* speed things up is if you make use of XML::Twig's ability to parse an XML file in chunks, instead of reading the whole thing into memory like you're currently doing.
use warnings;
use strict;
use IO::Uncompress::Gunzip ();
use XML::Twig;
my $z = IO::Uncompress::Gunzip->new('in.xml.gz')
or die "gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
my $twig = XML::Twig->new(
twig_roots => {
'/CatalogListings/Offer/EstimatedCPC' => sub {
my ($t, $elt) = @_;
print $elt->text*100, "\n";
$t->purge;
},
},
);
$twig->parse($z);
$z->close;
This produces the same output as before, but discards each <EstimatedCPC> element when it's done processing it, and ignores the other elements.
(* The code works, but I haven't had the chance to do a performance test.)
Hope this helps, -- Hauke D |