在perl中使用XML::Simple与Data::Dumper对XML文件进行操作。
foo.xml文件,内容如下:
<servers>
<server>
<name>sahara</name>
<osname>solaris</osname>
<osversion>2.6</osversion>
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server>
<name>gobi</name>
<osname>Linux</osname>
<osversion>2.0.34</osversion>
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</servers>
pxml_001.pl为处理脚本
tiger506@darkstar:~/Program/Perl$ cat ./pxml_001.pl
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xml_file="/home/tiger506/Program/Perl/foo.xml";
my $serv_info=XMLin($xml_file);
print Dumper($serv_info);
print "Server-sahara\'s os version is: ".$serv_info->{server}->{sahara}->{osversion}."\n";
执行结果如下:
tiger506@darkstar:~/Program/Perl$ ./pxml_001.pl
$VAR1 = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '2.0.34',
'osname' => 'Linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
Server-sahara's os version is: 2.6