Parsing in a long XML file with perl -


hi im new perl programmer , im trying fetch data long xml file. cant 2 data @ same time in general code, please need check how use efficiently loop or structure data need.

<item>     <datetime>7/28/2016 12:00:00 - 12:00:15 am</datetime>     <datetime_raw>42579.1668402778</datetime_raw>     <value channel="traffic total (volume)" channelid="1">4,664,204 kbyte</value>     <value_raw channel="traffic total (volume)" channelid="1">4776145337.3504</value_raw>     <value channel="traffic total (speed)" channelid="1">517,319 kbit/s</value>     <value_raw channel="traffic total (speed)" channelid="1">64664843.4518</value_raw>     <value channel="traffic dl (volume)" channelid="2">3,805,763 kbyte</value>     <value_raw channel="traffic dl (volume)" channelid="2">3897101197.8596</value_raw>     <value channel="traffic dl (speed)" channelid="2">422,107 kbit/s</value>     <value_raw channel="traffic dl (speed)" channelid="2">52763352.2591</value_raw>     <value channel="traffic ul (volume)" channelid="3">858,442 kbyte</value>     <value_raw channel="traffic ul (volume)" channelid="3">879044139.4907</value_raw>     <value channel="traffic ul (speed)" channelid="3">95,212 kbit/s</value>     <value_raw channel="traffic ul (speed)" channelid="3">11901491.1927</value_raw>     <coverage>100 %</coverage>     <coverage_raw>0000010000</coverage_raw>    </item> 

i have hundrends of items these , need extrac pair datatime , specific value channel="traffic total (volume)" @ same time, here extract of perl code:

my $reader = xml::libxml::reader->new(string => "$hdf") or die "cannot read file.xml\n";  while ($reader->nextelement( 'item' )) {                     $item = $reader->readinnerxml;                     while ($reader->nextelement( 'datetime' )) {                         $dt = $reader->readinnerxml;                         print $dt;                          while ($reader->nextelement( 'value' )) {                             $value = $reader->readinnerxml;                             if ($value eq 'traffic total (speed)'){                                 $hd = $reader->readinnerxml;                                 print $hd;                             }                         }                     }                 } 

thanks comments it.

for long xml, find xml::twig has got - can use twig_handers , purge you're parsing, can handle subsets of xml efficiently.

so assuming you're wanting go "item":

#!/usr/bin/env perl  use strict; use warnings;  use xml::twig;  @things = ( './datetime', './value[@channel="traffic total (speed)"]' );  sub process_item {    ( $twig, $item ) = @_;          print join "\t", (map { $item -> get_xpath($_,0) -> text } @things),"\n";    $twig->purge; }  $twig = xml::twig -> new ( twig_handlers => { 'item' => \&process_item } );    $twig -> parsefile ('your_file.xml');  

what purge discard 'up point' memory, making quite efficient xml containing large number of similar elements.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -