1

I am using the following script:

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

use strict;
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.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
Hedley Phillips
  • 389
  • 1
  • 8
  • 16

2 Answers2

7

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.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • Ah, have just spotted this in the doc: "Names in XML must begin with a letter. The remaining characters may be letters, digits, hyphens (-), underscores (_) or full stops (.). It is also allowable to include one colon (:) in an element name but this should only be used when working with namespaces (XML::Simple can only usefully work with namespaces when teamed with a SAX Parser). But couldn't see it say that I couldn't use an url in 36pt red font so I missed it. Thanks for your help. – Hedley Phillips Sep 19 '11 at 12:22
  • 1
    @Hedley, the text you quote talks about the input to `XMLout()`. I was referring to the documentaiton of `XMLin()` (which is what you're trying to use): "The XML specifier can be one of the following: A filename [...], undef [...], A string of XML [...], An IO::Handle object [...]." No mentioning of URL in there. – Kusalananda Sep 19 '11 at 12:29
5

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 ));
Marco De Lellis
  • 1,169
  • 6
  • 10