Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

how do i parse xml webpage in perl

by mipatel (Novice)
on May 08, 2012 at 03:20 UTC ( #969359=perlquestion: print w/ replies, xml ) Need Help??
mipatel has asked for the wisdom of the Perl Monks concerning the following question:

hello Currently i am able to parse the xml file if it is saved in my folder from the webpage.

use strict; use warnings; use Data::Dumper; use XML::Simple; my $parser = new XML::Simple; my $data = $parser->XMLin("config.xml"); print Dumper($data);

But it does not work if i am trying to parse it from the website.

use strict; use warnings; use Data::Dumper; use XML::Simple; my $parser = new XML::Simple; my $data = $parser->XMLin("http://website/computers/computers_main/con +fig.xml"); print Dumper($data);

it gives me following error "File does not exist: http://website/computers/computers_main/config.xml at test.pl line 12"

How do I parse multiple xml files from the webpage? i have to grab multiple xml form the websites and parse it. can someone please help me with this?

Comment on how do i parse xml webpage in perl
Select or Download Code
Re: how do i parse xml webpage in perl
by bitingduck (Friar) on May 08, 2012 at 03:57 UTC

    XML::Simple doesn't work from a URL-- it doesn't know how to find something on the web by itself. You need to use something like LWP to get the web page first and put it in a string or file, then do your stuff with XML::Simple

    One word of warning, is that unless you know the XML is well formed, you might be happier using an HTML processor, like HTML::Treebuilder or HTML::TokeParser. XML parsers are supposed to die horribly if they get something that's badly formed, but the HTML parsers are usually more forgiving.

      HTML parsers are usually more forgiving.

      Yeah. More forgiving at parsing HTML.

      XML is not HTML. And you usually don't want parsing XML to be that forgiving.

      -sauoq
      "My two cents aren't worth a dime.";
        I didn't notice that he actually was pulling down an XML file-- I thought he was trying to parse a webpage with an XML parser..
Re: how do i parse xml webpage in perl
by sauoq (Abbot) on May 08, 2012 at 03:58 UTC
    But it does not work if i am trying to parse it from the website.

    It isn't supposed to. Did you even glance at the docs?

    You'll need to use another module to fetch your content. Maybe try something like:

    use strict; use warnings; use Data::Dumper; use XML::Simple; use LWP::Simple; my $parser = new XML::Simple; my $data = $parser->XMLin( get( "http://website/computers/computers_ma +in/config.xml" )); print Dumper($data);
    -sauoq
    "My two cents aren't worth a dime.";
Re: how do i parse xml webpage in perl
by choroba (Abbot) on May 08, 2012 at 12:30 UTC
    XML::Simple can not work with remote files. There are XML modules that can, i.e. XML::LibXML:
    use XML::LibXML; my $x = XML::LibXML->load_xml(location => "http://java.sun.com/developer/earlyAccess/xml/examples/samples/bo +ok-order.xml");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (17)
As of 2014-10-30 16:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls