in reply to Re^3: Parsing XML w/ Attributes
in thread Parsing XML w/ Attributes
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> ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Parsing XML w/ Attributes
by jrsimmon (Hermit) on Dec 20, 2007 at 15:53 UTC | |
by Jenda (Abbot) on Dec 20, 2007 at 18:04 UTC |