1

I have this code

#!/usr/bin/perl -w

use strict;
use URI;
use LWP::UserAgent;
use Data::Dumper;
use XML::Simple;

my $DBVersion = '';

my $url = URI->new('https://example.com');

$url->query_form(
    'sql' => 'select email,firstname from account for xml auto',
    'DBVersion' => $DBVersion
    );

my $response = LWP::UserAgent->new->get($url);
die "Error: ", $response->status_line unless $response->is_success;

print $response->content;

And now I would like to use XML::Simple on $response->content.

From can I see that they use

my $doc = $xs1->XMLin($file);

foreach my $key (keys (%{$doc->{species}})){
   print $doc->{species}->{$key}->{'common-name'} . ' (' . $key . ') ';
   print $doc->{species}->{$key}->{conservation}->final . "\n";
}

But my XML data in not in a file, but in an object created by the LWP module.

How can I parse that data with XML::Simple?

1 Answer 1

2

XMLin() can accept a string of XML as a parameter:

use XML::Simple;
my $ref = XMLin($response->content);

See perldoc XML::Simple for the details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.