I (newbie alert) have problems to parse information from a XML document. I have a device (called OW-SERVER) that reads sensor values from a 1wire sensor network and provides the readings in a XML document. The XML typically looks as follows:
<?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>
I want to use XML::LibXML in order to read the values from each indvidual sensor. However, the following perl script was not successful:
#!/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();
}
Any help would be appreciated. Daniel
The problem is using a default namespace without giving it a name to use in queries.
From the XML::LibXML::Node documentation for findnodes
:
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.
Namespaces, especially a default one, and XPath just don't play nicely without some work.
Using XML::LibXML::XPathContext and assigning a name to the namespace is one of the suggested approaches. Like so:
#!/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();
}