我正在尝试从XML文件中提取数据。
XML的格式如下:
<notifications>
<notification name="ccmSmtp" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="ccmSmtp"/>
</objects>
<description>
This is a description
</description>
</notification>
<notification name="ccmAlertGroup" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="callHome"/>
</objects>
<description>
This is a description
</description>
</notification>
<notification name="ccmAlert" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="callHome"/>
</objects>
<description>
This is a description
</description>
</notification>
<notification name="ccmSmtp" oid="1.3.6.1" status="current">
<objects>
</objects>
<description>
This is a description
</description>
</notification>
</notifications>
我编写了以下代码以从example.xml文件中提取通知节点。
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("example.xml",KeyAttr => {
notifications => notification => 'name'
});
$notification = $data->{notifications};
print Dumper($notification);
当我运行上面的Perl文件时,我得到以下输出:
$VAR1 = {
'notification' => [
{
'objects' => {
'object' => {
'name' => 'ccmSmtp',
'module' => 'callhome'
}
},
'status' => 'current',
'oid' => '1.3.6.',
'name' => 'ccmSmtp',
'description' => ' This is a mib '
},
{
'objects' => {
'object' => {
'name' => 'callHome',
'module' => 'module'
}
},
'status' => 'current',
'oid' => '1.3.6.1.4',
'name' => 'ccmAlert',
'description' => 'This is a description'
},
{
'objects' => {
'object' => {
'name' => 'callHome',
'module' => 'homemib'
}
},
'status' => 'current',
'oid' => '1.3.6.1.4',
'name' => 'ccmAlertf',
'description' => 'This is a description'
},
{
'objects' => {},
'status' => 'current',
'oid' => '1.3.6.1',
'name' => 'ccmSmtp',
'description' => ' This is an example'
}
]
};
我的问题是,如何提取通知节点的内容并将值存储在单独的变量/数组中?
答案 0 :(得分:1)
通常,从XML中提取项目我会从XPath开始。 Perl有一个XPath包,甚至可能在你已经使用的XML包中。
答案 1 :(得分:1)
$notification
只是一个散列的引用,其中包含一个具有哈希数组的“通知”键。
你可以通过
循环它for my $n ( @{$notification->{notification}} ) {
# ie. to get status out, this is same for description,oid,name
my $status = $n->{status};
# To get the nested 'objects' data object module value (phew)
my $object_module = $n->{status}{object}{module};
}
答案 2 :(得分:0)
我不清楚你想要什么。您发布的代码没有生成该输出,因为它不会编译,但如果您编写了
my $data = $xml->XMLin("x.xml", KeyAttr => ['notification']);
print Dumper $data;
那么你会得到那个。现在$ data-&gt; {notification}是对四个哈希(对应于四个元素)的数组的引用,可以作为访问
my $note0 = $data->{notification}[0];
等。这是否回答了你的问题?
答案 3 :(得分:0)
我会像下面一样:
foreach $NOTIFICATION( @{$data->{'notification}})
{
foreach $OBJECT (@{$NOTIFICATION->{'OBJECT'}})
{
$name=$NOTIFICATION->{'name'}; $module=$OBJECT->{'module'};
Print $name , $module
}
}