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
asを取得しようとしていますBactrian Camel
)
print $root->children('species')->first_child_text('common-name');