在Twig中打印XML的内容
发布时间:2020-12-16 22:53:05 所属栏目:百科 来源:网络整理
导读:我试图在Perl中打印一些基本日志,但我遇到了一个非常简单的问题:我无法打印 XML标签的内容. my $twig=XML::Twig-new(pretty_print = "nice");$twig-parse($xml);my $root = $twig-root;my @desc=$root-descendants_or_self('node');my $nrofdesc=@desc;my $
我试图在Perl中打印一些基本日志,但我遇到了一个非常简单的问题:我无法打印
XML标签的内容.
my $twig=XML::Twig->new(pretty_print => "nice"); $twig->parse($xml); my $root = $twig->root; my @desc=$root->descendants_or_self('node'); my $nrofdesc=@desc; my $sentence = $root->descendants('sentence')->print; my $sentenceid = $root->{att}->{id}; if ($nrofdesc > $maxdescendants) { print "$sentencet$nrofdesct$sentenceidn"; } 我试过上面的代码,但收到错误
这是哪一行: my $sentence = $root->descendants('sentence')->print; 我也尝试过经常提出的文本,但是我得到了同样的错误.我在这里错过了什么? 解决方法
这不是jQuery; – (你必须遍历后代列表.
另外你不能使用print来收集变量中的数据,你使用print来打印!使用sprint代替: $sentence= join '',map { $_->sprint } $root->descendants('sentence'); 如果您想要的是元素的文本,并且所有句子元素的内容都是纯文本,您还可以使用$sentence = $root-> findvalue(‘// sentence’) 此外,使用$root-> att(‘id’)或$root-> id,因为$root-> {att} – > {id}不是官方API的一部分,并且可能会更改未来. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |