我尝试为每个标记调用子例程,但是
end_tag_handlers
从不调用。
我的目标是一个这样的序列:
---序列---
什么时候
<auto>
呼叫
\&loading
.
什么时候
<apps><title>
呼叫
\&kicks
.
什么时候
<apps><logs>
呼叫
\&bye
.
什么时候
<apps>
呼叫
\&app
.
什么时候
<应用程序标题
呼叫
踢腿
.
什么时候
<应用程序日志
呼叫
再见
.
什么时候
<Apps≫
呼叫
应用程序
.
什么时候
</auto>
呼叫
\&finish
. 渐次
没有打电话。
温度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";
}
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---