我想获得一个特定的字符串,例如< received> 123< / received>中的123从 一些将从URL中检索的XML。
我已经编写了一段代码,但却遇到了错误信息:
尝试在/usr/share/perl5/XML/Twig.pm第392行祝福参考。
我该如何解决?
代码:
use XML::Twig;
use LWP::Simple;
my $url = 'http://192.168.1.205:13000/status.xml';
my $twig = new XML::Twig(TwigRoots => {
'smsc/received' => sub {$author = $_[1]->text; }});
$twig->nparse( $url );
$twig->print;
答案 0 :(得分:5)
nparse为你处理new
(因此是'n'),在这种情况下你想要的可能是xparse
,或者只是让模块弄清楚并执行此操作:< / p>
my $url= 'http://192.168.1.205:13000/status.xml';
my $twig= XML::Twig->parse( twig_roots =>
{ 'smsc/received' => sub { $author= $_[1]->text;}},
$url
);
$twig->print; # I am not sure why you print the twig instead of just $author
答案 1 :(得分:3)
似乎是 nparse 方法中的错误,因为如果用以下内容替换该行:
$twig->parse( LWP::Simple::get($url) );
然后你应该发现它工作正常(或者当我尝试它时)。
/ I3az /