私の質問は、XML:Twigのハンドラーにいくつかの引数を渡す方法と、ハンドラーから結果を返す方法です。
これが私のコードで、ハードコーディングされています。
<counter name = "music", report type = "month", stringSet index = 4>
。
引数を使用してこれを実装する$counter_name
方法$type
、、$id
?string_listの結果を返す方法は?ありがとう(申し訳ありませんが、ここにxmlファイルを投稿しませんでした。問題が発生したためです。<および>内のすべては無視されます)。
use XML::Twig;
sub parse_a_counter {
my ($twig, $counter) = @_;
my @report = $counter->children('report[@type="month"]');
for my $report (@report){
my @stringSet = $report->children('stringSet[@index=”4”]');
for my $stringSet (@stringSet){
my @string_list = $stringSet->children_text('string');
print @string_list; # in fact I want to return this string_list,
# not just print it.
}
}
$counter->flush; # free the memory of $counter
}
my $roots = { 'counter[@name="music"]' => 1 };
my $handlers = { counter => \&parse_a_counter };
my $twig = new XML::Twig(TwigRoots => $roots,
TwigHandlers => $handlers);
$twig->parsefile('counter_test.xml');