svankalken has asked for the wisdom of the Perl Monks concerning the following question:
I can read it no problem using XML::Simple. Where I have problems is that the filename portion can have a different number of entries per server element. In other words, just as the example above, server 1 has fewer entries than server 2. If they have the same number of entries I can simple use<config> <server> <name>server1</name> <file> <filename>/etc/named.conf</filename> <filename>/etc/nsswitch.conf</filename> </file> </server> <server> <name>server2</name> <file> <filename>/etc/named.conf</filename> <filename>/etc/nsswitch.conf</filename> <filename>/etc/hosts</filename> </file> </server> </config>
...which isn't particularly elegant, but works well. I'm having trouble accessing the hash elements as a loop within in the loop. Any ideas? My sample code to do some of this is below:{server}->{file}->{filename}->[0] {server}->{file}->{filename}->[1]
use XML::Simple; use Data::Dumper; $xml = new XML::Simple (KeyAttr=>[]); $data = $xml->XMLin("xml.xml"); #print Dumper($data); foreach $machine (@{$data->{server}}) { $name=$machine->{name}; print "$name\n"; $file1=$machine->{file}->{filename}->[0]; $file2=$machine->{file}->{filename}->[1]; $file3=$machine->{file}->{filename}->[2]; print "\t$file1 $file2 $file3\t\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML understanding
by GrandFather (Saint) on Aug 22, 2006 at 00:09 UTC | |
|
Re: XML understanding
by imp (Priest) on Aug 22, 2006 at 00:07 UTC | |
|
Re: XML understanding
by izut (Chaplain) on Aug 22, 2006 at 11:35 UTC |