Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Retrieve specific xml content from given url

by conandor (Initiate)
on Oct 06, 2009 at 09:41 UTC ( [id://799416]=perlquestion: print w/replies, xml ) Need Help??

conandor has asked for the wisdom of the Perl Monks concerning the following question:

I want to get a specific string, eg. 123 in <received>123</received> from an XML that will retrieve from url. I have write a code but stuck with an error msg:
Attempt to bless into a reference at /usr/share/perl5/XML/Twig.pm line + 392.
any idea to solve it? The code
use XML::Twig; use LWP::Simple; my $url = 'http://mywebserver/status.xml'; my $twig = new XML::Twig(TwigRoots => { 'smsc/received' => sub {$author = $_[1]->text; }}); $twig->nparse( $url ); $twig->print;

Replies are listed 'Best First'.
Re: Retrieve specific xml content from given url
by jakobi (Pilgrim) on Oct 06, 2009 at 09:47 UTC

    Please include a bit more information/markup.

    • please check the node of your question and update the formatting (<code>, ...)
    • 192.168.x.x is private, so a short version of status.xml might be interesting.

    Thanx
    Peter

    Update: thanx for fixing the formatting:). WRT my 'private IP range' in the URL you initially provided: can we have an example of the input as well (mywebserver's still not on the internet)?

Re: Retrieve specific xml content from given url
by Anonymous Monk on Oct 06, 2009 at 10:46 UTC
    nparse is a constructor, use xparse
      Here is example
      #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; use LWP::Simple qw( $ua ); $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); my $url = 'http://search.cpan.org/uploads.rdf'; my $twig = XML::Twig->nparse_pp( TwigRoots => { 'item/title' => sub{}, }, $url ); $twig->print; __END__
      and example output
      <?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:admin="http://webns.ne +t/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xml +ns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org +/1999/02/22-rdf-syntax-ns#" xmlns:syn="http://purl.org/rss/1.0/module +s/syndication/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/ +"> <title>Pod-Markdown-0.01</title> <title>WWW-Nike-NikePlus-0.03</title> <title>Coro-5.2</title> <title>App-War-0.01</title> <title>JavaScript-Packer-0.03_01</title> <title>File-Path-2.08</title> <title>Task-BeLike-hanekomu-0.01</title> </rdf:RDF>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://799416]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found