我试图从XML文件中提取数据。使用Perl提取多级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'
}
]
};
我的问题是,我怎么能提取通知节点的内容和值存储在独立的变量/数组?
通过代码运行的代码提示给出了下面的输出“不是一个数组引用” – haiprasan86 2011-04-19 10:04:38
上述更新的样本,现在试试吧。 – dalton 2011-04-19 10:11:07