Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML::Simple question

0 views
Skip to first unread message

Mike Blezien

unread,
Dec 22, 2009, 10:54:32 AM12/22/09
to Perl List
Hello,

were using the XML/Simple module to process a XML response using the code below.
But for some reason the module can't read the $xmlresponse data unless we create
a temp file first to store the data then pass that to the module. Is there a way
to do this without having to create a temp file first. ?? Everything does work,
the response is return. We used the Data/Dumper module to verify the data first.

=========================================================
my $ua = new LWP::UserAgent;
$ua->timeout(10);
$ua->agent("HTTP/1.1");
my $req = new HTTP::Request 'POST' => "$xmlscript";
$req->content_type("application/x-www-form-urlencoded");
$req->content($xmlrequest);
my $res = $ua->request($req);

my $error = $res->is_error();
my $success = $res->is_success();

# Data returned in XML Format
my $xmlresponse = $res->content();

my $simple = new XML::Simple(KeyAttr=>[]);
# read XML file
my $xmldata = $simple->XMLin("$xmlresponse");
# can't read the response in this manner
===================================================

Thanks and happy holidays,

Mike(mickalo)Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
http://www.thunder-rain.com/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Robert Wohlfarth

unread,
Dec 22, 2009, 6:47:00 PM12/22/09
to Perl List
On Tue, Dec 22, 2009 at 9:54 AM, Mike Blezien <mic...@frontiernet.net>wrote:

> were using the XML/Simple module to process a XML response using the code
> below. But for some reason the module can't read the $xmlresponse data
> unless we create a temp file first to store the data then pass that to the
> module. Is there a way to do this without having to create a temp file
> first. ?? Everything does work, the response is return. We used the
> Data/Dumper module to verify the data first.
>
> =========================================================
> my $ua = new LWP::UserAgent;
> $ua->timeout(10);
> $ua->agent("HTTP/1.1");
> my $req = new HTTP::Request 'POST' => "$xmlscript";
> $req->content_type("application/x-www-form-urlencoded");
> $req->content($xmlrequest);
> my $res = $ua->request($req);
>
> my $error = $res->is_error();
> my $success = $res->is_success();
>
> # Data returned in XML Format
> my $xmlresponse = $res->content();
>
> my $simple = new XML::Simple(KeyAttr=>[]);
> # read XML file
> my $xmldata = $simple->XMLin("$xmlresponse");
> # can't read the response in this manner
> ===================================================
>

Try the "parse_string" method instead. The documentation says that XMLin
guesses based on the presence of "<" and ">" characters. It appears that
XMLin guesses wrong in your case.

"parse_string" explicitly expects an XML string. So there's no logic to
confuse.

--
Robert Wohlfarth

Mike Blezien

unread,
Dec 22, 2009, 7:23:10 PM12/22/09
to Robert Wohlfarth, Perl List

Thanks Robert, that did the trick :)

Mike(mickalo)Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing

Custom Programming & Web Hosting Services
http://www.thunder-rain.com/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Jenda Krynicky

unread,
Dec 22, 2009, 7:44:02 PM12/22/09
to Perl List
From: "Mike Blezien" <mic...@frontiernet.net>

> Hello,
>
> were using the XML/Simple module to process a XML response using the code below.
> But for some reason the module can't read the $xmlresponse data unless we create
> a temp file first to store the data then pass that to the module. Is there a way
> to do this without having to create a temp file first. ?? Everything does work,
> the response is return. We used the Data/Dumper module to verify the data first.
>
> =========================================================
> my $ua = new LWP::UserAgent;
> $ua->timeout(10);
> $ua->agent("HTTP/1.1");
> my $req = new HTTP::Request 'POST' => "$xmlscript";

DO NOT QUOTE VARIABLES!

If the $variable already contains a string, you just unnecessary make
a copy. If it contains a number (well it can contain both, but I mean
the case when it contains just the number), you force Perl to convert
the number to string (and store that string alongside the number in
the variable) and make a copy of the string. And possibly later it
will have to convert the string back to number.

And these are the cases when it actually works, even if it's
inefficient. As soon as you quote like this a variable that contains
a reference or an object (a reference bless()ed to a package), you
end up with useles string, that will just look like a reference when
printed out. Do not quote variables!

my $req = new HTTP::Request 'POST' => $xmlscript;

is enough!

> my $simple = new XML::Simple(KeyAttr=>[]);
> # read XML file
> my $xmldata = $simple->XMLin("$xmlresponse");

OK, you are safe from automatic key related transformations ... what
about tags with optional attributes? Or tags that are sometimes but
not always repeated?

See http://www.perlmonks.org/?node_id=697036

Jenda
===== Je...@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Mike Blezien

unread,
Dec 23, 2009, 6:08:50 AM12/23/09
to Jenda Krynicky, Perl List

----- Original Message -----
From: "Jenda Krynicky" <Je...@Krynicky.cz>
To: "Perl List" <begi...@perl.org>
Sent: Tuesday, December 22, 2009 6:44 PM
Subject: Re: XML::Simple question

Jenda,

thanks for the info, I nomarlly don't quote variables like that, just didn't
catch that one.

0 new messages