开发者

perl script- to read many values from xml file

how to read the multiple values from XML file using perl script? i have the xml file like:

<Provisioning>
<Applianc开发者_开发技巧e>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.230</IPAddress>
</Appliance>
<Appliance>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.530</IPAddress>
</Appliance>
<Appliance>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.730</IPAddress>
</Appliance>...
</Provisioning>

and i have written the code like:

use XML::Simple;
use Data::Dumper;
my $xml = new XML::Simple;
my $peermas = $xml->XMLin($masapplications);
print "file contents: $peermas \n";
print Dumper($peermas);
@masipaddr =+ $peermas->{Appliance}->{IPAddress};      #{Provisioning}->{Appliance}->{IPAddress};
print "The MAS ip: @masipaddr \n";

i am very new to perl script and my code can read only one IP address not the remaining 2. so what should i do in this case?? please reply soon... thanks in advance.


You already have all info you need in your $peermas. But if you need array of your IP addressed you may use:

my @massipaddr = map { $_->{IPAddress} } @{ $peermas->{Appliance} };

This map iterate on array of hashes $peermas->{Appliance} and push each IPAddress from it into @massipaddr.


Something like this perhaps:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Simple;

my $xml = join '', <DATA>;

my $peermas = XMLin($xml);

foreach (@{$peermas->{Appliance}}) {
  print $_->{IPAddress}. "\n";
}

__DATA__
<Provisioning>
<Appliance>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.230</IPAddress>
</Appliance>
<Appliance>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.530</IPAddress>
</Appliance>
<Appliance>
        <ID>1</ID>
        <SiteID></SiteID>
        <IPAddress>10.52.32.730</IPAddress>
</Appliance>...
</Provisioning>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜