Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please help - this is surely basic stuff. I have been able to use XML::RSS to parse remote RSS files no problem but now all I want to do is parse a remote XML file... and have been unsuccessful so far having tried all sorts of XML modules. Can anyone give example code please to show how to parse an XML file that is delivered from another site? Thanks in advance

Replies are listed 'Best First'.
Re: Read XML from remote server
by erroneousBollock (Curate) on Sep 24, 2007 at 16:23 UTC
    Download the file with LWP::UserAgent.

    Parse it with XML::Simple or XML::Parser.

    XML::Simple example:

    use strict; use warnings; use XML::Simple; use LWP::UserAgent; use Data::Dumper; my $ua = new LWP::UserAgent; my $response = $ua->get('http://server/path/to.xml'); die "couldn't fetch xml\n" unless $response && $response->is_success +; my $xmlString = $response->content; my @options = (); # put parser options in here. my $ref = XMLin($xmlString, @options); print Dumper($ref);

    -David

      Fantastic repy, thanks a lot! I never tried using useragent so it looks like that made the difference for me.
Re: Read XML from remote server
by amarquis (Curate) on Sep 24, 2007 at 18:45 UTC

    What XML modules have you tried and what problems have you been having? Any of the common roadblocks I can think of you've probably already dealt with getting your remote RSS parser working.

    Edit:It appears the solution got posted while I'd this window up and was eating lunch :). Mind my asking how you went about grabbing the remote RSS in your past project?

      Sure, CPAN had a perfect example but that used LWP::Simple instead - I just hadn't connected the two examples I found. All the XML::Simple etc examples were all using xml files local to the server rather than a remote one which needs LWP.