我尝试为每个标记调用子例程,但从end_tag_handlers不调用它.
我的目标是这个序列:
--- ---序列
时<auto>调用\&loading.
当<apps><title>通话\&kicks.
当<apps><logs>通话\&bye.
当<apps>通话\&app.
当<apps><title>通话\&kicks.
当<apps><logs>通话\&bye.
当<apps>通话\&app.
当</auto>通话\&finish.→ 没有打电话.
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";
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
我想在这里更多.
---finish---
Run Code Online (Sandbox Code Playgroud)
来自XML :: Twig的文档:
end_tag_handlers哈希{expression => \&handler}.设置元素关闭时调用的元素处理程序(在XML :: Parser End处理程序的末尾).处理程序用2个参数调用:树枝和元素的标记.
twig_handlers在完全解析元素时调用,为什么有这个冗余选项?只有一个用途end_tag_handlers:当使用该twig_roots选项时,触发根之外的元素的处理程序.
您正在为auto元素设置结束处理程序,它是根.而你只使用twig_roots了apps.所以永远不会调用结束处理程序.
您应该使用twig_handlers而不是安装处理程序.
试试这个:
my $twig = XML::Twig->new(
start_tag_handlers =>
{ 'auto' => \&loading
},
twig_handlers =>
{ 'apps/title' => \&kicks,
'apps/logs' => \&bye,
'auto' => \&finish
},
twig_roots =>
{ 'apps' => \&app
},
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
243 次 |
| 最近记录: |