如何从 XML::Simple 的数据结构中提取值?
我试图弄清楚如何解析前 2 个值中的
值(37%
和 61.8F) 在此 XML 中。我无法弄清楚,因为它们似乎具有相同的字段名称并且都在“传感器”下...任何帮助将不胜感激...
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);
我正在阅读的 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>
谢谢这么多!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您已经得到了答案,这里有一些您可能会觉得有用的链接。
他们很好地介绍了如何使用引用和复杂的数据结构,并提供了大量示例。
食谱
我并不是想轻蔑地向您抛出 RTFM,请不要以这种方式看待我的回答。 Perl 有很多文档,刚开始时可能很难找到解决它们的方法。 How to RTFM 是一个很好的资源,有助于揭开手册的神秘面纱。
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.
Cookbook
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.
我想这就是你想要的。
I think this is what you want.