私の目的はstart_tag_handler
(以下を参照) /タグが見つかったときにapps
/title
コンテンツを取得することです (以下のサンプル XML を参照)。apps
title
/タグ
が見つかったときに/コンテンツをend_tag_handler
取得します。apps
logs
apps
logs
しかし代わりに、このコードは null を返して終了します。
これは解析用の Perl コードです ( XML::Twigを使用)###:
#!/usr/local/bin/perl -w
use XML::Twig;
my $twig = XML::Twig->new(
start_tag_handlers =>
{ 'apps/title' => \&kicks
},
twig_roots =>
{ 'apps' => \&app
},
end_tag_handlers =>
{ 'apps/logs' => \&bye
}
);
$twig -> parsefile( "doc.xml");
sub kicks {
my ($twig, $elt) = @_;
print "---kicks--- \n";
print $elt -> text;
print " \n";
}
sub app {
my ($twig, $apps) = @_;
print "---app--- \n";
print $apps -> text;
print " \n";
}
sub bye {
my ($twig, $elt) = @_;
print "bye \n";
print $elt->text;
print " \n";
}
これは doc.xml### です:
<?xml version="1.0" encoding="UTF-8"?>
<auto>
<apps>
<title>watch</title>
<commands>set,start,00:00,alart,end</commands>
<logs>csv</logs>
</apps>
<apps>
<title>machine</title>
<commands>down,select,vol_100,check,line,end</commands>
<logs>dump</logs>
</apps>
</auto>
これはコンソールの出力です###:
C:\>perl parse.pl
---kicks---
---app---
watchset,start,00:00,alart,endcsv
---kicks---
---app---
machinedown,select,vol_100,check,line,enddump