Purpose: I've hundreds of compressed (.gz) xml files from which I've to create distribution of values of 'EstimatedCPC' tags from xml files. Issue: I'm not sure how to read gzipped file in xml::twig; I don't want to unzip each file then read. Is there any way to read directly? Please help. Also code:2 is throwing "Wide character in print at /usr/local/share/perl/5.22.1/XML/Twig.pm line 8628." this error. How to fix this?
Perl Code 1:
Perl Code 2:my $file = 'file.xml'; my $twig = new XML::Twig; ## Get twig object $twig->parsefile($file); ## parse the file to build twig my $root = $twig->root; ## Get the root element of twig my @elements = $root->children; ## Get elements list of twig foreach my $e (sort @elements){ my $cpc = ($e->first_child('EstimatedCPC')->text)*100; print $cpc,"\n"; }
Example of xml:$twig->parsefile( "file.xml"); # build the twig my $root= $twig->root; # get the root of the twig (stats) my @players= $root->children; # get the player list # sort it on the text of the field my @sorted= sort { $b->first_child( $field)->text <=> $a->first_child( $field)->text } @players; print '<?xml version="1.0"?>'; # print the XML declaration print '<!DOCTYPE stats SYSTEM "stats.dtd" []>'; print '<stats>'; # then the root element start tag foreach my $player (@sorted) # the sorted list { $player->print; # print the xml content of the elemen +t print "\n"; } print "</stats>\n"; # close the document
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <CatalogListings> <Offer id="af94bdd18ff9ffbf66afb5286dcb68fa"> <Command>new</Command> <Title>Puma Pitch Shorts</Title> <Description>Pitch Shorts: Let your football team look like pr +os and play like pros with these lightweight shorts from PUMA. Highly + functional materials draw sweat away from your skin and help keep yo +u dry and comfortable during exercise. Get ready for dry with dryCELL +. Bio-based wicking finish to keep you dry.</Description> <EstimatedCPC>0.0434</EstimatedCPC> <LastModified>2017-02-15 21:31:41</LastModified> <Images> <Image available="true"> <Url>http://r.kelkoo.com/r/uk/11210623/100353523/90/90 +/http%3A%2F%2Fpumaecom.scene7.com%2Fis%2Fimage%2FPUMAECOM%2F702075_25 +_01_EEA%3F%24PUMA_GRID%24/d4qCltxt.0XARAbgLGRcGsAKxgSY3iHhaVcF_7bEuPg +-</Url> </Image> </Images> <Url>http://ecs-uk.kelkoo.co.uk/ctl/go/offersearchGo?.ts=14872 +45527057&.sig=Ch1dMBKSr5hhrL8bNhlNkv_GMSg&catId=100353523& +;localCatId=100353523&comId=11210623&offerId=af94bdd18ff9ffbf +66afb5 286dcb68fa&searchId=null&affiliationId=96951977&country=uk +&wait=true&contextLevel=2&service=11</Url> <MobileFriendly>false</MobileFriendly> <Merchant id="11210623"/> <Category id="100353523"> <Name>Miscellaneous</Name> </Category> <Price currency="GBP"> <Price>20.0</Price> <DeliveryCost>3.95</DeliveryCost> <TotalPrice>23.95</TotalPrice> </Price> <ProductClass>0</ProductClass> <Availability>1</Availability> <OffensiveContent>false</OffensiveContent> <Ean>4055261425365</Ean> <MerchantCategory>Male|Mens Sports Football Pants & Sho +rts</MerchantCategory> <Brand>Puma</Brand> <BrandId>2571</BrandId> <Model>Pitch Shorts</Model> <Currency>GBP</Currency> </Offer> </CatalogListings>
In reply to How to read compressed (gz) file in xml::twig by CSharma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |