如何在 Perl 中使用 XML::Simple 解析多记录 XML 文件

发布于 2024-09-04 07:41:59 字数 1395 浏览 3 评论 0原文

我的 data.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>10.0</price>
  </cd>
  <cd country="CHN">
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.99</price>
  </cd>
  <cd country="USA">
    <title>Hello</title>
    <artist>Say Hello</artist>
    <price>0001</price>
  </cd>
</catalog>

我的 test.pl

#!/usr/bin/perl

   # use module
   use XML::Simple;
   use Data::Dumper;

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

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

   # access XML data
   print "$data->{cd}->{country}\n";
   print "$data->{cd}->{artist}\n";
   print "$data->{cd}->{price}\n";
   print "$data->{cd}->{title}\n";

输出:

Not a HASH reference at D:\learning\perl\t1.pl line 16.

评论:我用谷歌搜索并找到了这篇文章(处理单个 xml 记录)。 http://www.go4expert.com/forums/showthread.php?t=第812章 我用文章代码进行了测试,它在我的笔记本电脑上运行得很好。

然后我在上面创建了练习代码来尝试访问多个记录。但失败了。我该如何修复它?谢谢。

My data.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>10.0</price>
  </cd>
  <cd country="CHN">
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.99</price>
  </cd>
  <cd country="USA">
    <title>Hello</title>
    <artist>Say Hello</artist>
    <price>0001</price>
  </cd>
</catalog>

my test.pl

#!/usr/bin/perl

   # use module
   use XML::Simple;
   use Data::Dumper;

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

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

   # access XML data
   print "$data->{cd}->{country}\n";
   print "$data->{cd}->{artist}\n";
   print "$data->{cd}->{price}\n";
   print "$data->{cd}->{title}\n";

Output:

Not a HASH reference at D:\learning\perl\t1.pl line 16.

Comment: I googled and found the article(handle single xml record).
http://www.go4expert.com/forums/showthread.php?t=812
I tested with the article code, it works quite well on my laptop.

Then I created my practice code above to try to access multiple record. but failed. How can I fix it? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑 2024-09-11 07:41:59

始终使用严格;,始终使用警告; 不要像您所做的那样引用复杂的引用。您使用 Dumper 是正确的;它应该向您显示 cd 是一个数组引用 - 您必须指定哪个 cd。

#!/usr/bin/perl
use strict;
use warnings;

# use module
use XML::Simple;
use Data::Dumper;

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

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

# access XML data
print $data->{cd}[0]{country};
print $data->{cd}[0]{artist};
print $data->{cd}[0]{price};
print $data->{cd}[0]{title};

Always use strict;, always use warnings; Don't quote complex references like you're doing. You're right to use Dumper;, it should have shown you that cd was an array ref - you have to specificity which cd.

#!/usr/bin/perl
use strict;
use warnings;

# use module
use XML::Simple;
use Data::Dumper;

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

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

# access XML data
print $data->{cd}[0]{country};
print $data->{cd}[0]{artist};
print $data->{cd}[0]{price};
print $data->{cd}[0]{title};
天赋异禀 2024-09-11 07:41:59

如果你执行print Dumper($data),你会发现数据结构并不像你想象的那样:

$VAR1 = {
          'cd' => [
                  {
                    'country' => 'UK',
                    'artist' => 'Bonnie Tyler',
                    'price' => '10.0',
                    'title' => 'Hide your heart'
                  },
                  {
                    'country' => 'CHN',
                    'artist' => 'Dolly Parton',
                    'price' => '9.99',
                    'title' => 'Greatest Hits'
                  },
                  {
                    'country' => 'USA',
                    'artist' => 'Say Hello',
                    'price' => '0001',
                    'title' => 'Hello'
                  }
                ]
        };

你需要像这样访问数据:

print "$data->{cd}->[0]->{country}\n";
print "$data->{cd}->[0]->{artist}\n";
print "$data->{cd}->[0]->{price}\n";
print "$data->{cd}->[0]->{title}\n";

If you do print Dumper($data), you will see that the data structure does not look like you think it does:

$VAR1 = {
          'cd' => [
                  {
                    'country' => 'UK',
                    'artist' => 'Bonnie Tyler',
                    'price' => '10.0',
                    'title' => 'Hide your heart'
                  },
                  {
                    'country' => 'CHN',
                    'artist' => 'Dolly Parton',
                    'price' => '9.99',
                    'title' => 'Greatest Hits'
                  },
                  {
                    'country' => 'USA',
                    'artist' => 'Say Hello',
                    'price' => '0001',
                    'title' => 'Hello'
                  }
                ]
        };

You need to access the data like so:

print "$data->{cd}->[0]->{country}\n";
print "$data->{cd}->[0]->{artist}\n";
print "$data->{cd}->[0]->{price}\n";
print "$data->{cd}->[0]->{title}\n";
☆獨立☆ 2024-09-11 07:41:59

除了 Evan 所说的之外,如果您不确定是否被一个或多个元素困住,ref() 可以告诉你它是什么,你可以相应地处理它:

my $data = $xml->XMLin("file.xml");

if(ref($data->{cd}) eq 'ARRAY')
{
   for my $cd (@{ $data->{cd} })
   {
      print Dumper $cd;
   }
}
else # Chances are it's a single element
{
   print Dumper $cd;
}

In addition to what has been said by Evan, if you're unsure if you're stuck with one or many elements, ref() can tell you what it is, and you can handle it accordingly:

my $data = $xml->XMLin("file.xml");

if(ref($data->{cd}) eq 'ARRAY')
{
   for my $cd (@{ $data->{cd} })
   {
      print Dumper $cd;
   }
}
else # Chances are it's a single element
{
   print Dumper $cd;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文