为什么 XML::Twig 将提取的字符串输出两次?
为什么我的字符串在输出中出现两次?
#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig;
my $string = '<cd_catalogue><title>Hello, World!</title></cd_catalogue>';
my $t= XML::Twig->new( twig_handlers => { cd_catalogue => \&cd_catalogue, },
pretty_print => 'indented',
);
$t->parse( $string );
sub cd_catalogue {
my( $t, $cd_catalogue ) = @_;
$cd_catalogue->flush;
}
# Output:
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
Why do I get my string two times in the output?
#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig;
my $string = '<cd_catalogue><title>Hello, World!</title></cd_catalogue>';
my $t= XML::Twig->new( twig_handlers => { cd_catalogue => \&cd_catalogue, },
pretty_print => 'indented',
);
$t->parse( $string );
sub cd_catalogue {
my( $t, $cd_catalogue ) = @_;
$cd_catalogue->flush;
}
# Output:
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的子程序更改为使用
print
和purge
而不是flush
可以解决问题:flush
由于以下原因而变得混乱您的示例很简单,因为cd_catalogue
是根节点。如果您将数据更改为如下所示:或者如果您更改了 twig_handler 以查找
title
:那么您会发现
$cd_catalogue->flush
现在按预期工作与您的$string
。/I3az/
Changing your sub to use
print
andpurge
instead offlush
gets around problem:The
flush
is getting confused because of the simplicity of your example becausecd_catalogue
is root node. If you change your data to something like this:or if you changed your twig_handler to look for
title
:then you will find that
$cd_catalogue->flush
now works as expected with your$string
./I3az/
您的程序错误地使用了 XML::Twig。 根据文档,您应该“始终冲洗树枝,而不是一个元素。”
将
cd_catalogue
更改为以获得预期的行为。
Your program uses XML::Twig incorrectly. According to the documentation, you should "always flush the twig, not an element."
Change
cd_catalogue
toto get the expected behavior.