On 12/30/05, Mario Sanchez <[EMAIL PROTECTED]> wrote:
hello

if you please,

my perl  program is;

use XML::Simple;
use Data::Dumper;
#create object
$xml = new XML::Simple;

$xml = new XML::Simple(
      ForceArray => [ qw(server address) ],
      KeyAttr => 'name'
      );

#read XML file
$data = "">#access XML data
print Dumper($data);
exit;


and "sever.xml" contains
<config>
<server name="sahara" osname="solaris" osversion="2.6">
  <address> 10.0.0.101</address>
  <address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
  <address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
  <address> 10.0.0.103</address>
  <address>10.0.1.103</address>
</server>
</config>

the program as is runs great. no problem...

Making the change above will insure that <address> is converted into an array even when only one element is present (e.g., server "gobi"), for uniform processing (and that <server> is made a hash based on the "name" attribute, in case there is only one server defined).

question:
instead of using the DUMPER,  how can i access ONE field of one element
inside perl? for example, if i want to know what is the address for server
'gobi',  how to i get to it??

Here's an example of accessing the data:

foreach my $server (keys %{$data->{server}})
{
# long version
   print "Server $server (OS=$data->{server}->{$server}->{osname}, Ver=$data->{server}->{$server}->{osversion})\n";

# create a reference to one server hash entry for simplified access:
   my $srvr = $data->{server}->{$server};
   print "Server $server (OS=$srvr->{osname}, Ver=$srvr->{osversion})\n";

   foreach my $addr (@{$srvr->{address}})
   {
      print "   ADDR=$addr\n";
   }
}

--
pDale Campbell
"All of the birds are laughing!
 C'mon, let's all join in."
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to