0
$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";
}

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

4

1 回答 1

2

首先,请在您的程序中使用strict和。warnings他们将帮助您追踪错误。

我不确定你在挣扎什么。我已经注释掉了 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";
}
于 2013-02-05T23:24:04.543 回答