如何使用XML :: Twig从URL中提取一些XML数据? [英] How can I extract some XML data from a URL using XML::Twig?
本文介绍了如何使用XML :: Twig从URL中提取一些XML数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取一个特定的字符串,例如从中的< received> 123</received>中的123. 一些将从URL检索的XML.
I want to get a specific string, for example 123 in <received>123</received> from some XML that will be retrieved from a URL.
我已经写了一个代码,但是遇到了错误消息:
I have write a code but stuck with an error message:
尝试在/usr/share/perl5/XML/Twig.pm第392行引用一个引用.
Attempt to bless into a reference at /usr/share/perl5/XML/Twig.pm line 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;
推荐答案
nparse为您处理new
(因此为'n'),在这种情况下,您想要的可能是xparse
,或者只是让该模块将其找出并执行以下操作:
nparse takes care of the new
for you (hence the 'n'), what you want in this case is probably xparse
, or just let the module figure it out and do this:
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
这篇关于如何使用XML :: Twig从URL中提取一些XML数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文