1

我(新手警报)在解析 XML 文档中的信息时遇到问题。我有一个设备(称为 OW-SERVER),它从 1wire 传感器网络读取传感器值并在 XML 文档中提供读数。XML 通常如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Devices-Detail-Response xmlns="http://www.embeddeddatasystems.com/schema/owserver" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PollCount>1069</PollCount>
<DevicesConnected>1</DevicesConnected>
<LoopTime>1.630</LoopTime>
<DevicesConnectedChannel1>1</DevicesConnectedChannel1>
<DevicesConnectedChannel2>0</DevicesConnectedChannel2>
<DevicesConnectedChannel3>0</DevicesConnectedChannel3>
<DataErrorsChannel1>1</DataErrorsChannel1>
<DataErrorsChannel2>0</DataErrorsChannel2>
<DataErrorsChannel3>0</DataErrorsChannel3>
<VoltageChannel1>4.87</VoltageChannel1>
<VoltageChannel2>4.88</VoltageChannel2>
<VoltageChannel3>4.78</VoltageChannel3>
<VoltagePower>5.09</VoltagePower>
<DeviceName>OWServer_v2-Enet</DeviceName>
<HostName>EDSOWSERVER2</HostName>
<MACAddress>00:04:A3:F8:5F:FE</MACAddress>
<DateTime>2020-10-24 19:27:08</DateTime>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>3E000005A0472628</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0.0000 Deg C</PrimaryValue>
<Temperature Units="Centigrade">0.0000</Temperature>
<UserByte1 Writable="True">0</UserByte1>
<UserByte2 Writable="True">0</UserByte2>
<Resolution>9</Resolution>
<PowerSource>0</PowerSource>
</owd_DS18B20>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>A4000005A0EC8128</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0.0000 Deg C</PrimaryValue>
<Temperature Units="Centigrade">0.0000</Temperature>
<UserByte1 Writable="True">0</UserByte1>
<UserByte2 Writable="True">0</UserByte2>
<Resolution>9</Resolution>
<PowerSource>0</PowerSource>
</owd_DS18B20>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>69000005A0046128</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0.0000 Deg C</PrimaryValue>
<Temperature Units="Centigrade">0.0000</Temperature>
<UserByte1 Writable="True">0</UserByte1>
<UserByte2 Writable="True">0</UserByte2>
<Resolution>9</Resolution>
<PowerSource>0</PowerSource>
</owd_DS18B20>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>B0000005A00F2528</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0.0000 Deg C</PrimaryValue>
<Temperature Units="Centigrade">0.0000</Temperature>
<UserByte1 Writable="True">0</UserByte1>
<UserByte2 Writable="True">0</UserByte2>
<Resolution>9</Resolution>
<PowerSource>0</PowerSource>
</owd_DS18B20>
<owd_DS2423 Description="RAM with counters">
<Name>DS2423</Name>
<Family>1D</Family>
<ROMId>D80000000FD6C41D</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0, 0</PrimaryValue>
<Counter_A>0</Counter_A>
<Counter_B>0</Counter_B>
</owd_DS2423>
<owd_DS2423 Description="RAM with counters">
<Name>DS2423</Name>
<Family>1D</Family>
<ROMId>ED0000000FC5741D</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0, 0</PrimaryValue>
<Counter_A>0</Counter_A>
<Counter_B>0</Counter_B>
</owd_DS2423>
<owd_DS2423 Description="RAM with counters">
<Name>DS2423</Name>
<Family>1D</Family>
<ROMId>B90000001013E31D</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0, 0</PrimaryValue>
<Counter_A>0</Counter_A>
<Counter_B>0</Counter_B>
</owd_DS2423>
<owd_DS2423 Description="RAM with counters">
<Name>DS2423</Name>
<Family>1D</Family>
<ROMId>830000000F97DB1D</ROMId>
<Health>0</Health>
<Channel>1</Channel>
<RawData>000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>0, 0</PrimaryValue>
<Counter_A>0</Counter_A>
<Counter_B>0</Counter_B>
</owd_DS2423>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>0004175171AFFF28</ROMId>
<Health>7</Health>
<Channel>1</Channel>
<RawData>57014B467FFF0C1038FF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>21.4375 Deg C</PrimaryValue>
<Temperature Units="Centigrade">21.4375</Temperature>
<UserByte1 Writable="True">75</UserByte1>
<UserByte2 Writable="True">70</UserByte2>
<Resolution>12</Resolution>
<PowerSource>255</PowerSource>
</owd_DS18B20>
</Devices-Detail-Response>

我想使用 XML::LibXML 来读取每个单独传感器的值。但是,以下 perl 脚本并不成功:

#!/usr/perl/bin

use warnings;
use strict;
use autodie;
use feature 'say';
use XML::LibXML;

my $file = 'doc.xml';

my $dom = eval {
    XML::LibXML->load_xml(location => $file);
};
if($@) {
    # Log failure and exit
    say "Error parsing $url";
    say "$@";
    say 'I will exit now.';
    exit 0;
}

say 'XML::LibXML has read the following:';
say $dom;

say '';
say 'Looking for Sensors:';
foreach my $sensor ($dom->findnodes('//owd_DS18B20')) {
    say 'found one!';
    say $sensor->to_literal();
}

任何帮助,将不胜感激。丹尼尔

4

1 回答 1

1

问题是使用默认命名空间而不给它一个名称以在查询中使用。

来自XML::LibXML::Node文档findnodes

关于 XPath 的一个常见错误是假设节点测试由一个元素名称组成,而没有前缀匹配默认命名空间中的元素。这个假设是错误的——根据 XPath 规范,这样的节点测试只能匹配不在(即 null)命名空间中的元素。

命名空间,尤其是默认命名空间和 XPath,如果不做一些工作就不能很好地发挥作用。

使用XML::LibXML::XPathContext并为命名空间分配名称是建议的方法之一。像这样:

#!/usr/bin/env perl
use warnings;
use strict;
use autodie;
use feature 'say';
use XML::LibXML;
use XML::LibXML::XPathContext;

my $file = 'doc.xml';

my $dom = eval {
    XML::LibXML->load_xml(location => $file);
};
if($@) {
    # Log failure and exit
    say "Error parsing $file";
    say "$@";
    say 'I will exit now.';
    exit 0;
}

say 'XML::LibXML has read the following:';
say $dom;

say '';
say 'Looking for Sensors:';
my $xpath = XML::LibXML::XPathContext->new($dom);
$xpath->registerNs("ow", "http://www.embeddeddatasystems.com/schema/owserver");
foreach my $sensor ($xpath->findnodes('//ow:owd_DS18B20')) {
    say 'found one!';
    say $sensor->to_literal();
}
于 2020-11-15T11:46:50.837 回答