#!/usr/local/bin/perl use XML::Parser; open(FILE, "cat ./machine-list.xml |"); $debug=0; $lines=""; while ( ) { next if /encoding/; $lines = "$lines$_"; } my $p = new XML::Parser ( Style => 'stream' ); $p->setHandlers( Start => \&start_handler, End => \&end_hander, Char => \&char_hander ); $p->parse($lines); sub start_handler { my ($parser, $elem, %attr) = @_; if ( $elem =~ /^License$/ ) { $ref = $attr{"ref"}; $num = $attr{"licenses"}; $num = 1 if ( not defined $num ); printf "%-10s => %10s * %d\n", $current_host, $ref, $num if ($debug >= 1); $license_list{$ref} += $num; } elsif ( $elem =~ /^Host$/ ) { $current_host=$attr{"name"}; } } sub char_hander { my ($parser, $string) = @_; unless ($string =~ /^\s+$/) { $current_char = $string; } } sub end_hander { my ($parser, $elem, %attr) = @_; if ( $elem =~ /^Host$/ ) { $current_host=null; } } foreach (keys %license_list) { printf "%-10s: %3d\n", $_, $license_list{$_}; }