加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用perl XML :: LibXML进行解析

发布时间:2020-12-15 23:26:45 所属栏目:大数据 来源:网络整理
导读:我使用perl的 XML :: Lib XML模块来解析来自设备的XML响应. 看来我能成功获取数据的唯一方法是修改设备的XML响应. 这是我对设备的XML响应: chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis"chassis junosstyle="inventory"nam
我使用perl的 XML :: Lib XML模块来解析来自设备的XML响应.
看来我能成功获取数据的唯一方法是修改设备的XML响应.
这是我对设备的XML响应:

<chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis">

<chassis junosstyle="inventory">

<name>Chassis</name>

<serial-number>JN111863EAFF</serial-number>

<description>VJX1000</description>

<chassis-module>

<name>Midplane</name>

</chassis-module>

<chassis-module>

<name>System IO</name>

</chassis-module>

<chassis-module>

<name>Routing Engine</name>

<description>VJX1000</description>

<chassis-re-disk-module>

<name>ad0</name>

<disk-size>1953</disk-size>

<model>QEMU HARDDISK</model>

<serial-number>QM00001</serial-number>

<description>Hard Disk</description>

</chassis-re-disk-module>

</chassis-module>

<chassis-module>

<name>FPC 0</name>

<chassis-sub-module>

<name>PIC 0</name>

</chassis-sub-module>

</chassis-module>

<chassis-module>

<name>Power Supply 0</name>

</chassis-module>

</chassis>

</chassis-inventory>

这是我用来解析和查找序列号的perl代码,例如:

#!/bin/env perl
use strict;
use warnings;
use XML::LibXML;
my $f = ("/var/working/xmlstuff");
sub yeah {
my $ff;
my $f = shift;
open(my $fff,$f);
while(<$fff>) {
$_ =~ s/^s+$//; 
$_ =~ s/^(<S+)s.*?=.*?((?:/)?>)/$1$2/g;
$ff .= $_;
}
close($fff);
return $ff
}
my $tparse = XML::LibXML->new();
my $ss = $tparse->load_xml( string => &yeah($f));
print map $_->to_literal,$ss->findnodes('/chassis-inventory/chassis/serial-number');

如果我不使用正则表达式替换,则不会加载任何脚本来解析.
我可以理解剥离换行符,但为什么我必须从XML响应中删除属性,所以它只适用于以下行:

<chassis-inventory xmlns="http://xml.juniper.net/junos/10.3D0/junos-chassis">

<chassis junosstyle="inventory">

成为这个:

<chassis-inventory>
<chassis>

>这是XML响应还是XML :: LibXML模块的问题?
>有没有办法让它忽略文件中没有使用正则表达式替换的空行这一事实?

谢谢您的帮助.

解决方法

XPATH表达式失败的原因是命名空间;你需要在上下文中搜索.这是 XML::libXML documentation的解释:

NOTE ON NAMESPACES AND XPATH:

A common mistake about XPath is to assume that node tests consisting
of an element name with no prefix match elements in the default
namespace. This assumption is wrong – by XPath specification,such
node tests can only match elements that are in no (i.e. null)
namespace.

So,for example,one cannot match the root element of an XHTML
document with $node->find(‘/html’) since ‘/html’ would only match if
the root element had no namespace,but all XHTML elements
belong to the namespace 07001. (Note that
xmlns=”…” namespace declarations can also be specified in a DTD,
which makes the situation even worse,since the XML document looks as
if there was no default namespace).

要处理此问题,请注册命名空间,然后使用命名空间搜索文档.这是一个适合您的示例:

#!/bin/env perl
use strict;
use warnings;
use XML::LibXML;

my $xml = XML::LibXML->load_xml( location => '/var/working/xmlstuff');
my $xpc = XML::LibXML::XPathContext->new($xml);
$xpc->registerNs('x','http://xml.juniper.net/junos/10.3D0/junos-chassis');

foreach my $node ($xpc->findnodes('/x:chassis-inventory/x:chassis/x:serial-number')) {

    print $node->textContent() . "n";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读