How to parse an XML page in perl

hello Currently, I can parse an xml file if it is saved in my folder from a web page.

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 this does not work if I try to parse it from a 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/config.xml");
print Dumper($data);

he gives me the following error "The file does not exist: http: //website/computers/computers_main/config.xml in the line test.pl 12"

How to process multiple XML files from a web page? I have to capture several xml-forms of sites and analyze them. can someone please help me with this?

+3
source share
3 answers

Super Edit: WWW:: Mechanize, , xml. , . .

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use WWW::Mechanize;

# Create a new instance of Mechanize
$bot = WWW::Mechanize->new();
# Create a cookie jar for the login credentials
$bot->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
    )
);
# Connect to the login page
$response = $bot->get( 'http://www.thePageYouLoginTo.com' );
# Get the login form
$bot->form_number(1);
# Enter the login credentials.
# You're going to have to change the login and 
# pass(on the left) to match with the name of the form you're logging
# into(Found in the source of the website). Then you can put your 
# respective credentials on the right.
$bot->field( login => 'thisIsWhereYourLoginInfoGoes' );
$bot->field( pass => 'thisIsWhereYourPasswordInfoGoes' );
$response =$bot->click();
# Get the xml page
$response = $bot->get( 'http://website/computers/computers_main/config.xml' );
my $content = $response->decoded_content();
my $parser = new XML::Simple;
my $data = $parser->XMLin($content);
print Dumper($data);

. LWP:: Simple, . (xml ) XMLin. : ​​ get $url. Edit2: , , .

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use LWP::Simple;

my $parser = new XML::Simple;

my $url = 'http://website/computers/computers_main/config.xml';
my $content = get $url or die "Unable to get $url\n";
my $data = $parser->XMLin($content);

print Dumper($data);
+2

XML::Simple. , XMLin , IO::Handle. , , URL HTTP.

Perl LWP::Simple, XML , , XMLin.

LWP::Simple cpan, , XML::Simple.

+3

Unless you have a specific reason to stick with XML :: Simple, use another parser, such as XML :: Twig, XML :: LibXML, which provides a built-in function for parsing XML available over the Internet.

Here is simple code for it using XML :: Twig

use strict;
use warnings;
use XML::Twig;
use LWP::Simple;

my $url = 'http://website/computers/computers_main/config.xml';
my $twig= XML::Twig->new();
$twig->parse( LWP::Simple::get( $url ));

As said, XML :: Simple does not have such a built-in function.

+1
source

All Articles