I am using XML::LibXML to parse a simple XML site. This issue I am running into is the following
Operation "+": no method found,
left argument in overloaded package XML::LibXML::NodeList,
right argument has no overloaded magic at check_server_room_temp l
+ine 13.
I am assuming it is because the getElementsByTagName wants an array but in my case there is only ever going to be 1 tag I am looking for. I've read through the documentation but find my self a little lost.
Here's the test code in question
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use XML::LibXML;
my $url = 'http://192.168.1.8/xmlfeed.rb';
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file($url);
my $temp = $doc->getElementsByTagName('currentReading');
my $math = $temp + 5;
print $math;
Simply printing out the variable temp properly prints the value I am looking for but does not allow me to run any conditional checks on it.
Here's the XML it's reading
<currentConditions>
<deviceName>Storm</deviceName>
<readingDateTime>02/26/2016 14:57:07</readingDateTime>
<tempUnits>Fahrenheit</tempUnits>
<form>1T</form>
<ports>
<port number="1" name="Port 1">
<condition type="temperature">
<currentReading>73.0</currentReading>
<highLimit>85</highLimit>
<lowLimit>10</lowLimit>
<alarmStatus>0</alarmStatus>
<prevAlarmStatus>0</prevAlarmStatus>
</condition>
</port>
</ports>
<errorReadingSensor>0</errorReadingSensor>
</currentConditions>
As always I appreciate the assistance.
Update ----
I may of found a solution but would still like to see if there is a cleaner way of accomplishing this task. I have done the following.
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use XML::LibXML;
my $url = 'http://192.168.1.8/xmlfeed.rb';
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file($url);
my @temp = $doc->getElementsByTagName('currentReading');
foreach(@temp){
my $temp = $_->to_literal;
my $math = $temp + 5;
print $math;
}
It would be nice to just get the value without looping especially when I know there is only ever 1 value