views:

207

answers:

2

I'm trying to figure out how to parse the <current></current> values from the first 2 (37% and 61.8F) in this XML. I can't figure it out since it seems that they have the same field names and are all under 'sensor'... any help would be appreciated...

Code in Perl:

#!/usr/bin/perl

use XML::Simple;
use Data::Dumper;
use LWP::Simple;


$url = 'http://localhostmachine/output/XML/output.xml';

# create object
my $xml = XML::Simple->new();

# read XML file
$data = $xml->XMLin(get($url));

# print output
print Dumper($data);

Output of XML file that I am reading in:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="wpman.xsl"?>
<watchport>
<title>
<TxtTitle>Location#1</TxtTitle>
</title>
<sensor>
<type>Combo Sensor (Humidity)</type>
<name>ComboSensor000</name>
<status>OK</status>
<current>37%</current>
<high>42 (10/19/2009 @ 04:05PM)</high>
<low>28 (10/17/2009 @ 11:26AM)</low>
</sensor>
<sensor>
<type>Combo Sensor (Temperature)</type>
<name>ComboSensor000</name>
<status>OK</status>
<current>61.6F</current>
<high>65.8 (10/17/2009 @ 11:26AM)</high>
<low>60.1 (10/19/2009 @ 04:00PM)</low>
</sensor>
<sensor>
<type> </type>
<name> </name>
<status> </status>
<current> </current>
</sensor>
<sensor>
<type>Updated: 10/19/2009 @ 05:47 PM</type>
<name>(Updated every 2 minutes)</name>
<status> </status>
<current> </current>
</sensor>
</watchport>

Thanks so much!

+6  A: 

I think this is what you want.

print $data->{sensor}[0]{current};
print $data->{sensor}[1]{current};
Tim
omg, it was that simple!? I was trying $data->{sensor[0]}Thank you so much Tim!
Josh G
+6  A: 

Since you've already got the answer, here are some links you might find helpful.

They give a good introduction to working with references and complex data structures, with plenty of examples.

I'm not trying to dismissively throw an RTFM at you, please don't take my response in that way. Perl has a lot of docs, it can be hard to find your way around them when starting out. How to RTFM is a good resource that helps demystify the manual.

daotoad
+1. I can vote again! ;-)
Sinan Ünür