开发者

XML::Simple doesn't seem to work with a URL. Is this correct?

I am using the following script:

#!/usr/local/bin/perl -wT

use s开发者_开发知识库trict;
use warnings;

print "Content-type: text/html\n\n";
print "xml reader";

# use module
use XML::Simple;
use Data::Dumper;

#print Dumper (XML::Simple->new()->XMLin());

and it will read in my xml file called xml.xml

If I now move the xml file out of my cgi-bin, change its name and reference it using:

#print Dumper (XML::Simple->new()->XMLin("../resource.xml"));

It still works.

If I now try and use a url instead the script doesn't return anything:

print Dumper (XML::Simple->new()->XMLin("http://digitalessence.net/resource.xml"));

I have tried with and without the http://, without the www and all sorts of different ways of doing this but it doesn't return anything.

Have I done something silly here or will it just not load a remote url?

Thanks.


The XMLin() method in XML::Simple does not support fetching the XML from an URL. You would need to fetch the XML separately, either to a file or directly into a Perl scalar variable, before applying XMLin(). This is clear from the XML::Simple documentation.


You can modify your program as follows, using LWP to retrieve the remote resource:

[...]
# use module
use XML::Simple;
use LWP;
use Data::Dumper;

my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
my $res = $ua->request( $req );

print Dumper (XML::Simple->new()->XMLin( $res->content ));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜