Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Quick XML::LibXML question

by edimusrex (Monk)
on Feb 26, 2016 at 20:57 UTC ( #1156279=perlquestion: print w/replies, xml ) Need Help??

edimusrex has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Quick XML::LibXML question
by poj (Abbot) on Feb 26, 2016 at 21:35 UTC

      That's exactly what I was looking for! Thank you. I will read up on that to get a better understanding

      or

      my ( $temp ) = $doc->getElementsByTagName('currentReading');
      
Re: Quick XML::LibXML question
by tangent (Vicar) on Feb 26, 2016 at 23:00 UTC
    there is only ever going to be 1 tag I am looking for
    You can use an Xpath expression to find the value:
    my $value = $doc->findvalue('//currentReading');

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1156279]
Front-paged by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2021-09-22 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?