in reply to parse XML huge file using cpan modules
Try building a hash from the name/value elements within each resourceGroup. Example using XML::Twig
poj#!/usr/bin/perl use strict; use XML::Twig; #time|resourceGroup name|LCONNFAIL|LLOSTCONN|LIDLETIMEOUT|SIPADDR|SIPP +ORT my @columns = ('resourceGroup', 'LCONNFAIL','LLOSTCONN','LIDLETIMEOUT','SIPADDR','SIPPORT'); my $time; my %record = (); # tag handler my $twig = XML::Twig->new( twig_handlers => { resourceGroup => \&resourceGroup, statistic => \&statistic, }, start_tag_handlers => { statRecord => sub { $time = $_[1]->att('time') } } ); # process file print join '|','time',@columns; print "\n"; $twig->parsefile( 'my_file.xml' ); sub resourceGroup { my ($t,$e) = @_; $record{'resourceGroup'} = $e->att('name'); # print record print join "|",$time,map{$record{$_}}@columns; print "\n"; $t->purge; %record = (); } sub statistic { my ($t,$e) = @_; my $name = $e->first_child_text('name'); $record{$name} = $e->first_child_text('value'); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: parse XML huge file using cpan modules
by nicopelle (Acolyte) on Jul 28, 2019 at 17:26 UTC |