Question

I am trying to get text from a website that is HTTPS. I have made this work with LWP, but I need to parse the information because it is XML. I think I have found out how to do what I want with XML::LibXML but I cannot access the data from LWP::UserAgent with it.

This is my code:

#! usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use open qw(:std :utf8);
use 5.014;
use IO::Socket::SSL qw();
use XML::LibXML;

BEGIN {
    $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
    $ENV{HTTPS_DEBUG} = 1;
}

my $ua = LWP::UserAgent->new(ssl_opts => {
    SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
    verify_hostname => 0, 
});

my $response = $ua->get('https:<mywebsite>');

my $t = '';

if ( $response->is_success ) {
    $t = $response->decoded_content;
}
else {
    die $response->status_line;
}


my $parser = XML::->new();
my $xmldoc = $parser->parse_file($t);

print $xmldoc;

I am getting the error : No such file or directory and I get an error for every parser method I try and the LibXML parser string methods don't work because my data is many lines. I need a way to either trick XML::LibXML into thinking $t is a file or file handle or find another way to parse my data. And I don't want to actually create a file if that can be helped.

For reference this is the XML data I get from the HTTPS website with the above code that is stored in $t:

<?xml version="1.0" ?>
<resultset>
<table name="PROFILE">
 <column name="ID" type="String"/>
 <column name="VERSION" type="String"/>
 <column name="NAME" type="String"/>
 <column name="DESCRIPTION" type="String"/>
<data>
<r><c>0</c><c>1.0</c><c>Default profile</c><c>Default profile</c></r>
<r><c>2</c><c>1.2</c><c>Custom 2</c><c></c></r>
<r><c>3</c><c>6.0</c><c>Custom 3</c><c></c></r>
<r><c>1</c><c>1.15</c><c> For Compare</c><c>The built in profile for compare.</c></r>
<r><c>4</c><c>1.3</c><c>Custom 4</c><c> </c></r>
<r><c>6</c><c>11.0</c><c>Custom 6</c><c>Please only make approved changes.</c></r>
</data>
</table>
</resultset>

Any help is appreciated, thanks.

Was it helpful?

Solution

Just read the docs for XML::LibXML, and you'll see the alternative forms for calling it.

use XML::LibXML;
my $dom = XML::LibXML->load_xml(string => $t);

OTHER TIPS

If you look at the documentation for XML::LibXML::Parser you will see that the location option for load_xml can be either a path to a file or a URL. So there is no need to directly involve LWP at all; you can write just

my $xmldoc = XML::LibXML->load_xml(location => 'https:<mywebsite>');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top