如何在perl中获取xml值?

时间:2016-08-15 16:00:44

标签: xml 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.

为什么我会收到此错误,如何获取温度值?提前谢谢!

2 个答案:

答案 0 :(得分:4)

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 ;

答案 1 :(得分:2)

XML::Simple is discouraged

比你想象的容易得多 - 我会建议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,但您可能希望在其他方案中使用更具体的路径。)