in reply to XML parsing

I'm surprised you get as far as you do.

Your XML file is invalid -- the value of the 'name' attribute isn't quoted.

I'd suggest using Data::Dumper to print the structure that XMLin returned. (you've already loaded Data::Dumper, you might as well make use of it).

Replies are listed 'Best First'.
Re^2: XML parsing
by Anonymous Monk on May 09, 2005 at 22:36 UTC
    Sorry, it was mistyped in this form (i should've copy and pasted...). The name attribute is quoted in my xml file. So, the data in the dumped file is not that simple for me to read. How do I make sense of the following: $VAR1 = {
    'host' => {
    'jimmy.foo.com' => {
    'hardware' => 'eht0', 'eht1' ,
    'duty' => 'web'
    },
    The list goes on, but this is detailed info from one host. How do I make this useful. Thanks for being patient with me.

      When you're posting code on here, you really should wrap your copy/pasted items with <code> ... </code>. I'm guessing you probably saw:

      $VAR1 = { 'host' => { 'jimmy.foo.com' => { 'hardware' => ['eht0', 'eht1'], 'duty' => 'web' }, }, };

      So basically, you have a hash of hashes of hashes of something that might be an array. What you'll want to do is force them all to be an array (pass in ForceArray => [ qw(duty) ] to XMLin, or when you create a new XML::Simple object), or you'll have to check for each one:

      my $hosts = $data->{'host'}; foreach my $host ( keys %$hosts ) { my $duties = $hosts->{$host}->{'duty'}; print "$host : $_\n" foreach ( UNIVERSAL::isa( $duties, 'ARRAY' ) ? @$duties : $duties ); }