为什么 XML::Twig 不调用我的 end_tag_handler?
我尝试为每个标签调用子例程,但从未调用 end_tag_handlers
。
我的目标是这个序列:
---sequence---
当
调用\&loading
时。
当
temp.pl:
#!/usr/local/bin/perl -w
use XML::Twig;
my $twig = XML::Twig->new(
start_tag_handlers =>
{ 'auto' => \&loading
},
twig_handlers =>
{ 'apps/title' => \&kicks,
'apps/logs' => \&bye
},
twig_roots =>
{ 'apps' => \&app
},
end_tag_handlers =>
{ 'auto' => \&finish
}
);
$twig -> parsefile( "doc.xml");
sub loading {
print "---loading--- \n";
}
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";
}
sub finish {
print "---fishish--- \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 temp.pl
---loading---
---kicks---
watch
---bye---
csv
---app---
watchset,start,00:00,alart,endcsv
---kicks---
machine
---bye---
dump
---app---
machinedown,select,vol_100,check,line,enddump
我想在这里了解更多。
---finish---
I try to call subroutine for each tag, but the end_tag_handlers
is never invoked.
My aim is a this sequence:
---sequence---
when <auto>
call \&loading
.
when <apps><title>
call \&kicks
.
when <apps><logs>
call \&bye
.
when <apps>
call \&app
.
when <apps><title>
call \&kicks
.
when <apps><logs>
call \&bye
.
when <apps>
call \&app
.
when </auto>
call \&finish
. → It was not called.
temp.pl:
#!/usr/local/bin/perl -w
use XML::Twig;
my $twig = XML::Twig->new(
start_tag_handlers =>
{ 'auto' => \&loading
},
twig_handlers =>
{ 'apps/title' => \&kicks,
'apps/logs' => \&bye
},
twig_roots =>
{ 'apps' => \&app
},
end_tag_handlers =>
{ 'auto' => \&finish
}
);
$twig -> parsefile( "doc.xml");
sub loading {
print "---loading--- \n";
}
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";
}
sub finish {
print "---fishish--- \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>
output:
C:\>perl temp.pl
---loading---
---kicks---
watch
---bye---
csv
---app---
watchset,start,00:00,alart,endcsv
---kicks---
machine
---bye---
dump
---app---
machinedown,select,vol_100,check,line,enddump
I would like more here.
---finish---
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 XML::Twig 的文档中:
您正在为
auto
元素(即根元素)设置结束处理程序。而且您仅将twig_roots
用于应用
。所以最终处理程序永远不会被调用。您应该使用
twig_handlers
来安装您的处理程序。所以试试这个:
From the docs for XML::Twig:
You're setting up an end handler for your
auto
element, which is the root. And you're only usingtwig_roots
forapps
. So the end handler will never be called.You should install your handler using
twig_handlers
instead.So try this: