使用XML :: Simple迭代

时间:2013-02-05 22:01:22

标签: xml perl

$VAR1 = {
          'link' => [
                    {
                      'rel' => 'alternate',
                      'href' => 'http://www.test.com'
                    },
                    {
                      'rel' => 'self',
                      'href' => 'http://www.test.com'
                    }
                  ],
          'xmlns' => 'http://www.w3.org/2005/Atom',
          'entry' => {
                     'number=0001096906-13-000126' => {
                                                                                     'link' => {
                                                                                               'rel' => 'alternate',
                                                                                               'href' => 'http://www.test.com/1',
                                                                                               'type' => 'text/html'
                                                                                             },
                                                                                     'summary' => {
                                                                                                  'content' => 'Green',
                                                                                                  'type' => 'html'
                                                                                                },
                                                                                     'title' => 'Green Diamond',
                                                                                     'updated' => '2013-02-05T15:34:15-05:00',
                                                                                     'category' => {
                                                                                                   'scheme' => 'http://www.test.com/',
                                                                                                 }
                                                                                   },
             'number=0001096906-13-000130' => {
                                                                                     'link' => {
                                                                                               'rel' => 'alternate',
                                                                                               'href' => 'http://www.test.com/2',
                                                                                               'type' => 'text/html'
                                                                                             },
                                                                                     'summary' => {
                                                                                                  'content' => 'Green',
                                                                                                  'type' => 'html'
                                                                                                },
                                                                                     'title' => 'Green Diamond',
                                                                                     'updated' => '2013-02-05T15:34:15-05:00',
                                                                                     'category' => {
                                                                                                   'scheme' => 'http://www.test.com/',
                                                                                                 }
                                                                                   },
          'updated' => '2013-02-05T15:38:23-05:00',
          'author' => {
                      'email' => 'webmaster@test.com',
                      'name' => 'Webmaster'
                    },
          'id' => 'http://www.test.com',
          'title' => 'Latest Colors - Tue, 05 Feb 2013 15:38:23 EST'
        };

我的代码远远超过......

#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple;

# read XML file
$data = $xml->XMLin("sec_rss.xml");

# print output
#print Dumper($data);

foreach $e (@{$data->{entry}[0]}) {
      print $e->{link},"\n";
}

但我对如何遍历每个条目以获取元素感到困惑。有人能给我一些线索来帮助我滚动吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

首先,请在您的计划中使用strictwarnings。它们可以帮助您追踪错误。

我不确定你在挣扎着什么。我已经注释掉了部分XML数据结构,因为我认为解析它时出错或者你省略了什么。 entry键的最后一部分看起来好像缺少另一个键。

然后我演示了如何依次查看每个条目并访问其中的所有信息。

use warnings;
use strict;
use feature qw(say);
my $data = {
  'xmlns' => 'http://www.w3.org/2005/Atom',
  'link' => [
    {
      'rel' => 'alternate',
      'href' => 'http://www.test.com'
    },
    {
      'rel' => 'self',
      'href' => 'http://www.test.com'
    }
  ],
  'entry' => {
      'number=0001096906-13-000130' => {
      'link' => {
        'rel' => 'alternate',
        'href' => 'http://www.test.com/2',
        'type' => 'text/html'
      },
      'summary' => {
        'content' => 'Green',
        'type' => 'html'
      },
      'category' => {
        'scheme' => 'http://www.test.com/'
      },
      'updated' => '2013-02-05T15:34:15-05:00',
      'title' => 'Green Diamond'
    },
      'number=0001096906-13-000126' => {
      'link' => {
        'rel' => 'alternate',
        'href' => 'http://www.test.com/1',
        'type' => 'text/html'
      },
      'summary' => {
        'content' => 'Green',
        'type' => 'html'
      },
      'category' => {
        'scheme' => 'http://www.test.com/'
      },
      'updated' => '2013-02-05T15:34:15-05:00',
      'title' => 'Green Diamond'
    },
    #'title' => 'Latest Colors - Tue, 05 Feb 2013 15:38:23 EST',
    #'id' => 'http://www.test.com',
    #'author' => {
    #              'email' => 'webmaster@test.com',
    #              'name' => 'Webmaster'
    #            },
    #'updated' => '2013-02-05T15:38:23-05:00'
  }
};

foreach my $entry (keys %{ $data->{entry} }) {
  say "Key: ", $entry;
  say "Title: ", $data->{entry}->{$entry}->{title};
  say "Summary: ", $data->{entry}->{$entry}->{summary}->{content};
  say "Link: ", $data->{entry}->{$entry}->{link}->{href};
  say "Category: ", $data->{entry}->{$entry}->{category}->{scheme};
  say "Updated: ", $data->{entry}->{$entry}->{updated};
  print  "\n";
}