我的问题是:如何将一些参数传递给XML:Twig的处理程序,以及如何从处理程序返回结果。如何传递参数并使用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');
@ lilili07:欢迎来到SO。我对代码进行了格式化,您可以通过将其包含在反引号中来包含XML。如果我犯了一个错误,请随意编辑它。 – Zaid 2010-07-12 09:32:44