使用 Perl 提取多级 XML
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,为了从 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.
$notification
只是对具有一个键“通知”的哈希的引用,该哈希具有一组哈希。你可以通过这样做循环它
$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
我不清楚你想要什么。您发布的代码不会产生该输出,因为它不会编译,但如果您改为编写
,那么您将得到准确的结果。现在 $data->{notification} 是对四个散列(对应于四个元素)的数组的引用,可以通过
等方式访问它。这是否回答了您的问题?
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
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
etc. Does this answer your question?
我会像下面这样循环:
I would looped like below :