为什么';Txml::Twig调用我的结束标记处理程序?
我尝试为每个标记调用子例程,但从未调用为什么';Txml::Twig调用我的结束标记处理程序?,xml,perl,xml-twig,Xml,Perl,Xml Twig,我尝试为每个标记调用子例程,但从未调用end\u标记\u处理程序。 我的目标是实现这一顺序: ---顺序-- 当调用\&加载 当呼叫\&踢 当呼叫\&bye 当调用\&app 当呼叫\&踢 当呼叫\&bye 当调用\&app 当调用\&finish时。→ 它没有被调用。 临时损益: #!/usr/local/bin/perl -w use XML::Twig; my $twig = XML::Twig->new( start_tag_handlers =>
end\u标记\u处理程序。
我的目标是实现这一顺序:
---顺序--
当
调用\&加载
当
呼叫\&踢
当
呼叫\&bye
当
调用\&app
当
呼叫\&踢
当
呼叫\&bye
当
调用\&app
当
调用\&finish
时。→ 它没有被调用。
临时损益:
#!/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";
}
<?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
doc.xml:
#!/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";
}
<?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---
从以下文件:
end\u标记\u处理程序
散列{expression=>\&handler}。设置当元素被调用时调用的元素处理程序
已关闭(在XML::解析器结束处理程序的末尾)。用2调用处理程序
params:元素的细枝和标记
twig_处理程序
是在完全解析元素时调用的,那么为什么会有这种冗余呢
选项end\u tag\u处理程序只有一种用法
:使用twig\u roots
选项时
为根以外的元素触发处理程序
您正在为auto
元素(根元素)设置一个结束处理程序。而且你只在应用程序中使用细枝根。因此,永远不会调用结束处理程序
您应该使用twig\u处理程序安装处理程序
所以试试这个:
my $twig = XML::Twig->new(
start_tag_handlers =>
{ 'auto' => \&loading
},
twig_handlers =>
{ 'apps/title' => \&kicks,
'apps/logs' => \&bye,
'auto' => \&finish
},
twig_roots =>
{ 'apps' => \&app
},
);
非常感谢你,这真的是一项工作。之前我对XML::Twig有过疑问,我又读了一遍文档,唉,我的英语技能也有问题……现在我真的理解了XML::Twig文档中的start_tag_handlers部分了,看你的答案。似乎用户使用Twig_根的次数太多了。实际上我自己很少用它。我会在文档中添加一些关于它的内容。