我的问题是:如何将一些参数传递给XML:Twig的处理程序,以及如何从处理程序返回结果。
这是我的代码,硬编码:
<counter name = "music", report type = "month", stringSet index = 4>
。
如何使用参数$counter_name
,$type
,$id
来实现此目的?以及如何返回string_list的结果?谢谢(抱歉,我没有在这里发布xml文件,因为我有一些麻烦。&lt;和&gt;中的任何内容都被忽略了。)
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');
答案 0 :(得分:4)
将参数传递给处理程序的最简单,通常的方法是使用闭包。这是一个很大的词,但是一个简单的概念:你调用这样的处理程序tag => sub { handler( @_, $my_arg) }
和$my_arg
将被传递给处理程序。 Achieving Closure对这个概念有更详细的解释。
以下是我编写代码的方法。我使用Getopt::Long
进行参数处理,并使用qq{}
代替包含XPath表达式的字符串的引号,以便能够在表达式中使用引号。
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
use Getopt::Long;
# set defaults
my $counter_name= 'music';
my $type= 'month';
my $id= 4;
GetOptions ( "name=s" => \$counter_name,
"type=s" => \$type,
"id=i" => \$id,
) or die;
my @results;
my $twig= XML::Twig->new(
twig_roots => { qq{counter[\@name="$counter_name"]}
=> sub { parse_a_counter( @_, $type, $id, \@results); } } )
->parsefile('counter_test.xml');
print join( "\n", @results), "\n";
sub parse_a_counter {
my ($twig, $counter, $type, $id, $results) = @_;
my @report = $counter->children( qq{report[\@type="$type"]});
for my $report (@report){
my @stringSet = $report->children( qq{stringSet[\@index="$id"]});
for my $stringSet (@stringSet){
my @string_list = $stringSet->children_text('string');
push @$results, @string_list;
}
}
$counter->purge; # free the memory of $counter
}
答案 1 :(得分:1)
免责声明:我自己没有使用过Twig,所以这个答案可能不是惯用的 - 它是一个通用的“如何在回调处理程序中保持状态”的答案。
将信息传入和传出处理程序的三种方法是:
<强> ONE。国家在静态地点举行
package TwigState;
my %state = ();
# Pass in a state attribute to get
sub getState { $state{$_[0]} }
# Pass in a state attribute to set and a value
sub setState { $state{$_[0]} = $_[1]; }
package main;
sub parse_a_counter { # Better yet, declare all handlers in TwigState
my ($twig, $element) = @_;
my $counter = TwigState::getState('counter');
$counter++;
TwigState::setState('counter', $counter);
}
<强> TWO。 State以$ t(XML :: Twig对象)本身保存在某个“州”成员
中# Ideally, XML::Twig or XML::Parser would have a "context" member
# to store context and methods to get/set that context.
# Barring that, simply make one, using a VERY VERY bad design decision
# of treating the object as a hash and just making a key in that hash.
# I'd STRONGLY not recommend doing that and choosing #1 or #3 instead,
# unless there's a ready made context data area in the class.
sub parse_a_counter {
my ($twig, $element) = @_;
my $counter = $twig->getContext('counter');
# BAD: my $counter = $twig->{'_my_context'}->{'counter'};
$counter++;
TwigState::setState('counter', $counter);
$twig->setContext('counter', $counter);
# BAD: $twig->{'_my_context'}->{'counter'} = $counter;
}
# for using DIY context, better pass it in with constructor:
my $twig = new XML::Twig(TwigRoots => $roots,
TwigHandlers => $handlers
_my_context => {});
<强> THREE。使处理程序成为closure并使其保持状态
答案 2 :(得分:1)
最简单的方法是让__parse_a_counter__
返回一个sub(即闭包)并将结果存储在全局变量中。例如:
use strict;
use warnings;
use XML::Twig;
our @results; # <= put results in here
sub parse_a_counter {
my ($type, $index) = @_;
# return closure over type & index
return sub {
my ($twig, $counter) = @_;
my @report = $counter->children( qq{report[\@type="$type"]} );
for my $report (@report) {
my @stringSet = $report->children( qq{stringSet[\@index="$index"]} );
for my $stringSet (@stringSet) {
my @string_list = $stringSet->children_text( 'string' );
push @results, \@string_list;
}
}
};
}
my $roots = { 'counter[@name="music"]' => 1 };
my $handlers = { counter => parse_a_counter( "month", 4 ) };
my $twig = XML::Twig->new(
TwigRoots => $roots,
TwigHandlers => $handlers,
)->parsefile('counter_test.xml');
我使用以下XML测试了这个(这是我可以从您的示例XML和代码中解决的):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<counter name="music">
<report type="week">
<stringSet index="4">
<string>music week 4</string>
</stringSet>
</report>
</counter>
<counter name="xmusic">
<report type="month">
<stringSet index="4">
<string>xmusic month 4</string>
</stringSet>
</report>
</counter>
<counter name="music">
<report type="month">
<stringSet index="4">
<string>music month 4 zz</string>
<string>music month 4 xx</string>
</stringSet>
</report>
</counter>
</root>
我回来了:
[
[
'music month 4 zz',
'music month 4 xx'
]
];
这就是我所期待的!