http://www.perlmonks.org?node_id=969359

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?

Replies are listed 'Best First'.
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 bitingduck (Chaplain) 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 choroba (Cardinal) 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");