[英]How do i get an xml value in perl?
我是perl的初學者,我有一個我不知道如何解決的問題。 我試圖從網站獲取xml數據並將溫度值放入數組中。
到目前為止,這是我的代碼:
use strict;
use warnings;
# For parsing
use XML::Simple;
# For downloading
use LWP::Simple;
# For debug output
use Data::Dumper;
# Turn off output buffering
$|=1;
# Note, no error checking here!
sub main
{
print "\nDownloading ....";
my $data = get('http://www.yr.no/place/Norway/Telemark/Sauherad/Gvarv/forecast_hour_by_hour.xml');
my $parser = new XML::Simple;
print "\nParsing ...";
my $dom = $parser->XMLin($data);
print "\n\n";
# Debug output.
#print Dumper($dom->{'forecast'});
# Data structure is a hash containing one key, 'entry'.
# Get the hash value and cast to an array.
my @entries = $dom->{'forecast'}->{'tabular'}->{'time'};
# Go through each array 'entry' element.
foreach my $entry(@entries)
{
print Dumper($entry);
# Each element is a hash.
# The band name can be got from one hash key.
my $tabular = $entry->{'temperature'};
#print "$tabular\n";
print "\n\n";
}
}
main();
我在終端上得到的輸出是:
},
{
'temperature' => {
'unit' => 'celsius',
'value' => '26'
},
'windSpeed' => {
'name' => 'Light breeze',
'mps' => '1.9'
},
'to' => '2016-08-17T16:00:00',
'pressure' => {
'unit' => 'hPa',
'value' => '1014.1'
},
'from' => '2016-08-17T15:00:00',
'precipitation' => {
'value' => '0'
},
'symbol' => {
'numberEx' => '1',
'var' => '01d',
'number' => '1',
'name' => 'Clear sky'
},
'windDirection' => {
'name' => 'North',
'code' => 'N',
'deg' => '4.4'
}
},
{
'precipitation' => {
'value' => '0'
},
'from' => '2016-08-17T16:00:00',
'windDirection' => {
'name' => 'North-northeast',
'code' => 'NNE',
'deg' => '20.6'
},
'symbol' => {
'number' => '1',
'name' => 'Clear sky',
'var' => '01d',
'numberEx' => '1'
},
'temperature' => {
'unit' => 'celsius',
'value' => '27'
},
'pressure' => {
'value' => '1013.4',
'unit' => 'hPa'
},
'to' => '2016-08-17T17:00:00',
'windSpeed' => {
'mps' => '1.2',
'name' => 'Light air'
}
},
{
'symbol' => {
'numberEx' => '1',
'var' => '01d',
'name' => 'Clear sky',
'number' => '1'
},
'windDirection' => {
'name' => 'East',
'code' => 'E',
'deg' => '83.0'
},
'from' => '2016-08-17T17:00:00',
'precipitation' => {
'value' => '0'
},
'windSpeed' => {
'mps' => '0.7',
'name' => 'Light air'
},
'pressure' => {
'unit' => 'hPa',
'value' => '1012.8'
},
'to' => '2016-08-17T18:00:00',
'temperature' => {
'unit' => 'celsius',
'value' => '27'
}
}
];
Not a HASH reference at script.pl line 43.
為什么會出現此錯誤,如何獲取溫度值? 提前致謝!
XML :: Simple告訴您不要使用它。 遵循其建議。
這是使用XML :: LibXML提取溫度的方法:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use XML::LibXML;
my $dom = 'XML::LibXML'->load_xml(
location => 'http://www.yr.no/place/Norway/Telemark/Sauherad/Gvarv/forecast_hour_by_hour.xml');
for my $temperature ($dom->findnodes('//temperature/@value')) {
say $temperature->getValue;
}
使用xsh ,它是XML :: LibXML的包裝,您可以編寫
open http://www.yr.no/place/Norway/Telemark/Sauherad/Gvarv/forecast_hour_by_hour.xml ;
for //temperature echo @value ;
比您想象的要容易得多-我建議使用XML::Twig
,因為我認為它的學習曲線更容易:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
use LWP::Simple;
my $twig = XML::Twig -> parse ( get('http://www.yr.no/place/Norway/Telemark/Sauherad/Gvarv/forecast_hour_by_hour.xml') );
#print the first temperature value:
print $twig -> get_xpath('//temperature',0 ) -> att('value'),"\n";
# all the temps in an array:
use Data::Dumper;
my @temps = map { $_ -> att('value') } $twig -> get_xpath('//temperature');
print Dumper \@temps;
這里我們利用xpath
搜索-有點像XML的正則表達式。 //
表示“樹中的任何位置”,因此實際上是對“溫度”節點的搜索。 (這似乎可以與您的XML一起使用,但是在其他情況下,您可能需要更具體的路徑)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.