如何使用 Perl 的 XML::Twig 从 XML 中提取子值?

发布于 2024-09-24 02:50:14 字数 1404 浏览 4 评论 0原文

我正在解析 XML 文件并尝试访问 XML 文件中的值。

#!/usr/bin/perl -w

use strict;
use XML::Twig;

my $file = 'files/camelids.xml';
print "File :: $file\n";
my $twig = XML::Twig->new();

$twig->parsefile($file);
# print "twig :: $twig\n";

my $root = $twig->root;
# print "root :: $root\n";

my $num = $root->children('species');
print "num :: $num\n\n\n";

print $root->children('species')->first_child_text('common-name');

示例 XML 文件是:

<?xml version="1.0"?>
<camelids>
  <species name="Camelus bactrianus">
    <common-name>Bactrian Camel</common-name>
    <physical-characteristics>
      <mass>450 to 500 kg.</mass>
      <appearance>
          <in-appearance>
              <inside-appearance>This is in inside appearance</inside-appearance>
          </in-appearance>  
      </appearance>
    </physical-characteristics>
  </species>
</camelids>

输出是:

File :: files/camelids.xml
num :: 1


Can't call method "first_child_text" without a package or object reference at xml-twig_read.pl line 19.

如何解决此问题?

这行代码有什么问题以及需要进行任何修改吗(这里我试图将common-name获取为Bactrian Camel

print $root->children('species')->first_child_text('common-name');

I am parsing the XML file and trying to access the values in XML file.

#!/usr/bin/perl -w

use strict;
use XML::Twig;

my $file = 'files/camelids.xml';
print "File :: $file\n";
my $twig = XML::Twig->new();

$twig->parsefile($file);
# print "twig :: $twig\n";

my $root = $twig->root;
# print "root :: $root\n";

my $num = $root->children('species');
print "num :: $num\n\n\n";

print $root->children('species')->first_child_text('common-name');

sample XML file is:

<?xml version="1.0"?>
<camelids>
  <species name="Camelus bactrianus">
    <common-name>Bactrian Camel</common-name>
    <physical-characteristics>
      <mass>450 to 500 kg.</mass>
      <appearance>
          <in-appearance>
              <inside-appearance>This is in inside appearance</inside-appearance>
          </in-appearance>  
      </appearance>
    </physical-characteristics>
  </species>
</camelids>

Output is:

File :: files/camelids.xml
num :: 1


Can't call method "first_child_text" without a package or object reference at xml-twig_read.pl line 19.

How to fix this issue?

Is there anything wrong in this line of code and any modification needed (here I am trying to get the common-name as Bactrian Camel)

print $root->children('species')->first_child_text('common-name');

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

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

发布评论

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

评论(2

自在安然 2024-10-01 02:50:14

将最后几行更改为

my @nums = $root->children('species');
print "num :: @nums\n\n\n";

foreach my $num (@nums) {
print $num->first_child_text('common-name');
}

Children 返回一个数组,因此您需要对其进行遍历。

为了帮助调试,请尝试以下操作:

my @nums = $root->children('species');
use Data::Dumper; #More debug information like this than a normal print
print Dumper @nums;

foreach my $num (@nums) {
print $num->first_child_text('common-name');
}

Change the last lines to

my @nums = $root->children('species');
print "num :: @nums\n\n\n";

foreach my $num (@nums) {
print $num->first_child_text('common-name');
}

children returns an array, so you need to run over it.

To help debugging, try this:

my @nums = $root->children('species');
use Data::Dumper; #More debug information like this than a normal print
print Dumper @nums;

foreach my $num (@nums) {
print $num->first_child_text('common-name');
}
晚风撩人 2024-10-01 02:50:14
use XML::Twig;

my $file = 't2.xml';

print "File :: $file\n";
# exit();
my $twig = XML::Twig->new();

$twig->parsefile($file);
# print "twig :: $twig\n";

my $root = $twig->root;
# print "root :: $root\n";

my @nums = $root->children('species');
*print "num :: " . @nums . "\n\n\n";*

foreach my $num (@nums){
   print $num->first_child_text('common-name') . *"\n";*
}
#exit();
use XML::Twig;

my $file = 't2.xml';

print "File :: $file\n";
# exit();
my $twig = XML::Twig->new();

$twig->parsefile($file);
# print "twig :: $twig\n";

my $root = $twig->root;
# print "root :: $root\n";

my @nums = $root->children('species');
*print "num :: " . @nums . "\n\n\n";*

foreach my $num (@nums){
   print $num->first_child_text('common-name') . *"\n";*
}
#exit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文