2011-04-19 62 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