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

I want to get a specific string, for example 123 in <received> 123 </received> from some XML that will be retrieved from the URL.

I wrote the code but got stuck with an error message:

Try to bless the link to / usr / share / perl 5 / XML / Twig.pm line 392.

How can I solve it?

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; 
+4
source share
2 answers

nparse takes care of new for you (hence the "n"), what you need in this case is probably xparse , or just let the module figure it out and do it:

 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 
+5
source

An error appears in the nparse method, because if you replace this line as follows:

 $twig->parse( LWP::Simple::get($url) ); 

Then you should find that it works fine (or it does when I try).

/ I3az /

+3
source

All Articles