我的目标是start_tag_handler(见下文)在找到应用程序/标题标签时获取应用程序/标题内容(请参阅下面的示例
XML).
和
end_tag_handler在找到apps / logs标记时获取应用程序/日志内容.
这是用于解析的Perl代码(使用XML::Twig)###:
#!/usr/local/bin/perl -w use XML::Twig; my $twig = XML::Twig->new( start_tag_handlers => { 'apps/title' => \&kicks },twig_roots => { 'apps' => \&app },end_tag_handlers => { 'apps/logs' => \&bye } ); $twig -> parsefile( "doc.xml"); 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"; }
这是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 parse.pl ---kicks--- ---app--- watchset,endcsv ---kicks--- ---app--- machinedown,enddump
查看
原文链接:https://www.f2er.com/xml/292107.htmlstart_tag_handlers
的XML :: Twig文档:
The handlers are called with 2 params: the twig and the element. The element is empty at that point,its attributes are created though.
在调用start_tag_handlers时,甚至还没有看到文本内容,因为解析开始标记(例如< title>,而不是结束标记< / title>)刚刚完成.
end_tag_handlers不提供元素文本的原因可能是对称:-).
您想要的可能是使用twig_handlers:
my $twig = XML::Twig->new( twig_handlers => { 'apps/title' => \&kicks,'apps/logs' => \&bye },twig_roots => { 'apps' => \&app },);
输出是:
---kicks--- watch bye csv ---app--- watchset,endcsv ---kicks--- machine bye dump ---app--- machinedown,enddump