使用 Perl 提取多级 XML

发布于 2024-11-02 08:21:31 字数 2688 浏览 2 评论 0原文

我正在尝试从 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'
      }
    ]
};

我的问题是,如何提取通知节点的内容并将值存储在单独的变量/数组中?

I'm trying to extract data from an XML file.

The format of the XML is as follows:

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

I have written the following code to extract the notifications node from example.xml file.

#!/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);

When I run the above Perl file I get the following output:

$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'
      }
    ]
};

My question is, how can I extract the contents of notification node and store the values in separate variables/array ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

调妓 2024-11-09 08:21:31

一般来说,为了从 XML 中提取项目,我会从 XPath 开始。有一个用于 Perl 的 XPath 包,甚至可能在您已经使用的 XML 包中。

Generally for extracting items from XML I would start with XPath. There is an XPath package for Perl, probably even in the XML package you're already using.

少女七分熟 2024-11-09 08:21:31

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

}

$notification is just a ref to a hash with one key 'notification' that has an array of hashes.

You could loop through it by doing

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

}
口干舌燥 2024-11-09 08:21:31

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

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

,那么您将得到准确的结果。现在 $data->{notification} 是对四个散列(对应于四个元素)的数组的引用,可以通过

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

等方式访问它。这是否回答了您的问题?

I am unclear what it is you want. The code you posted did not produce that output as it will not compile, but if you wrote instead

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

then you would get exactly that. Now $data->{notification} is a reference to an array of four hashes (corresponding to the four elements) which can be accessed as

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

etc. Does this answer your question?

柒夜笙歌凉 2024-11-09 08:21:31

我会像下面这样循环:

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

Print $name , $module 
}
}

I would looped like below :

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

Print $name , $module 
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文