2011-04-19 66 views
1

我试图从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' 
     } 
    ] 
}; 

我的问题是,我怎么能提取通知节点的内容和值存储在独立的变量/数组?

回答

1

通常用于从XML中提取项目,我将从XPath开始。有一个适用于Perl的XPath包,甚至可能在您已经使用的XML包中。

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}; 

} 
+0

通过代码运行的代码提示给出了下面的输出“不是一个数组引用” – haiprasan86 2011-04-19 10:04:38

+0

上述更新的样本,现在试试吧。 – dalton 2011-04-19 10:11:07

0

我不清楚什么是你想要的。你发布的代码没有产生输出,因为它不会编译,但如果你写代码

my $data = $xml->XMLin("x.xml", KeyAttr => ['notification']); 
print Dumper $data; 

然后你会得到这个。现在$数据 - > {通知}是四个散列(对应于四个元素)数组的引用可以如

my $note0 = $data->{notification}[0]; 

等这是否回答您的问题被访问?

+0

嗨鲍罗丁,我试着按照你的说法,但是通过打印$ note0不会给我任何输出。 :-( – haiprasan86 2011-04-19 09:52:13

+0

)您是否确定自己在每种情况下都有“通知”,而不是“通知”?您还应该在所写的所有内容的顶部“严格使用”和“使用警告”,以获取有关出现的任何错误的更好信息。 – Borodin 2011-11-08 06:55:56

0

我会循环象下面这样:

foreach $NOTIFICATION(@{$data->{'notification}}) 
{ 
foreach $OBJECT (@{$NOTIFICATION->{'OBJECT'}}) 
{ 
$name=$NOTIFICATION->{'name'};         $module=$OBJECT->{'module'}; 

Print $name , $module 
} 
} 
+0

您应该至少提供一些缩进并稍微解释您的解决方案... – Samoth 2013-08-22 12:00:22