我的目标是让下面的
当
但是,实际上这段代码返回null并退出。
以下是用Perl解析XML(使用XML::Twig)的代码:
start_tag_handler
在找到apps
/title
标签时获取其内容(见下面的示例XML)。当
end_tag_handler
找到apps
/logs
标签时,它将获取其内容。但是,实际上这段代码返回null并退出。
以下是用Perl解析XML(使用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,start,00:00,alart,endcsv
---kicks---
---app---
machinedown,select,vol_100,check,line,enddump