Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract some XML data from a URL using XML::Twig?

Tags:

xml

perl

xml-twig

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:

Attempt to bless into a reference at /usr/share/perl5/XML/Twig.pm line 392.

How can I solve it?

The code:

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;
like image 991
conandor Avatar asked Mar 01 '23 05:03

conandor


1 Answers

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
like image 154
mirod Avatar answered Mar 02 '23 19:03

mirod