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

In reply to Quick XML::LibXML question by edimusrex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.