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データ構造の一部をコメントアウトしました。これは、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 に答える