如何使用XML :: Twig从URL中提取一些XML数据?

con*_*dor 4 xml perl xml-twig

我想获取一个特定的字符串,例如来自某个XML的<received> 123 </ received>中的123,这些XML将从URL中检索.

我写了一段代码,但仍然遇到错误信息:

尝试在/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;
Run Code Online (Sandbox Code Playgroud)

mir*_*rod 5

nparse new为你处理(因此'n'),在这种情况下你想要的是xparse,或者只是让模块弄清楚并执行此操作:

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
Run Code Online (Sandbox Code Playgroud)