如何从XML :: Simple的数据结构中提取值?

0 xml perl xml-simple

我试图弄清楚如何解析这个XML中<current></current>前两个(37%61.8F)的值.我无法弄清楚,因为看起来他们有相同的字段名称并且都在'传感器'...任何帮助将不胜感激...

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);
Run Code Online (Sandbox Code Playgroud)

我正在阅读的XML文件的输出:

<?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>
Run Code Online (Sandbox Code Playgroud)

非常感谢!

Tim*_*Tim 7

我想这就是你想要的.

print $data->{sensor}[0]{current};
print $data->{sensor}[1]{current};
Run Code Online (Sandbox Code Playgroud)


dao*_*oad 7

既然你已经得到了答案,这里有一些你可能会发现有用的链接.

他们通过大量示例对使用引用和复杂数据结构进行了很好的介绍.

我不是在试图向您抛弃RTFM,请不要以这种方式接受我的回复.Perl有很多文档,在开始时很难找到他们的方法.如何使用RTFM是一个很好的资源,有助于揭开手册的神秘面纱.