I think it's safe to assume each host is there just once so it may be better to use ForceArray => ['process'] Which will somewhat simplify the code:
Also it may be better to use each() instead of foreach():#!/usr/bin/perl -w use strict; use XML::Simple; my $parser = new XML::Simple(ForceArray => ['process']); my $ref = $parser->XMLin('c:\temp\file.xml'); foreach my $host (keys(%$ref)){ foreach my $proc (keys(%{$ref->{$host}->{process}})){ print "$proc is running on $host\n"; } }
Or using a different module:#!/usr/bin/perl -w use strict; use XML::Simple; my $parser = new XML::Simple(ForceArray => ['process']); my $ref = $parser->XMLin('c:\temp\file.xml'); while (my ($host, $processes) = each %$ref){ foreach my $proc (keys(%{$processes->{process}})){ print "$proc is running on $host\n"; } }
use XML::Rules; my $parser = XML::Rules->new( rules => { process => sub { '@list' => $_[1]->{name}}, _default => sub { $_[0] => $_[1]->{list}}, config => 'pass no content', } ); my $data = $parser->parse(\*DATA); #use Data::Dumper; #print Dumper($data); foreach my $host (sort keys %$data) { foreach my $proc (@{$data->{$host}}) { print "$host runs $proc\n"; } } __DATA__ <?xml version="1.0" encoding="utf-8"?> <config> ...
In reply to Re^4: Parsing XML w/ Attributes
by Jenda
in thread Parsing XML w/ Attributes
by jpk236
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |