in reply to Calling nested containers with XML::Simple

You might want to revisist XML::Simple's documentation: forcearray => 1 coerces all nested elements into arrayrefs, turning something like...

$i->{snmp}->{version}

...into the less aesthetic...

$i->{snmp}->[0]->{version}->[0] # or $i->{snmp}[0]->{version}[0], which # reads more legibly to me (YMMV)

You might find a workaround in some of XML::Simple's other options, 'though.

    --k.


Replies are listed 'Best First'.
Re: Re: Calling nested containers with XML::Simple
by c (Hermit) on Jul 05, 2002 at 07:06 UTC
    Thanks! I agree that the second version is not very aesthetic whatsoever. I also found that when the containers are nested, they can be called via

    $i->{snmp}{version}

    i had been trying the syntax you show above with the second "->". i think that was the source of my frustration.

    thanks again, your post helped very much. -c

      Whenever i use 'forcearray', i tend to loop:
      for my $snmp (@{$i->{snmp}}) { for my $version (@{$snmp->{version}}) { print "$version\n"; } }
      Now, consider XML::XPath, which 'drills down' to the nodes in question:
      use XML::XPath; my $xp = XML::XPath->new(filename => 'filename'); my $nodeset = $xp->find('global/snmp/version'); print $_->string_value, "\n" for $nodeset->get_nodelist;
      Much nicer. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)