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

I'm bumping a previous question because I need to know if there are other alternatives out there since this is still happening

I currently am using the following code to parse XML from a URL,

use XML::LibXML; my $dom =XML::LibXML->new->load_xml(location => $URL);
but I will often get the following error:
parser error : Document is empty parser error : Start tag expected, '<' not found

The XML is fine when curling or loading in a browser and is properly formed. Again, this randomly will work and randomly won't work. My question is: Is there a way to either load this into memory first then parse, or is there a way we can determine why the parser is not finding the XML well formed? Is this a common problem when reading XML from a URL?

Replies are listed 'Best First'.
Re: XML::LibXML Parser Error
by hippo (Archbishop) on Mar 07, 2014 at 16:35 UTC

    The previous thread includes good discussion of why your initial HTTP request may fail. Are you testing the response codes or content yet?

      Yes, this still is experiencing the same issue, hence a fresh thread. I have tried to eval of the parser and this does not seem to make any difference. Attempting multiple times will occasionally work, but it can still work on the first try or fail after the 10th try. I need some other discussions on what else can be attempted and if this may be a common issue with a common solution. Has anyone else run into this issue before?

        Failure of HTTP requests hasn't been new since 1991 ;-) so test the response:

        use LWP; my $url = 'http://feeds.feedburner.com/oreilly/perl?format=xml'; my $agent = LWP::UserAgent->new; my $request = HTTP::Request->new(GET => $url); $request->content_type('application/xml'); my $response = $agent->request($request); if ($response->is_success) { print "HTTP response is good\n"; # Parse XML here } else { die "Awooga! HTTP request failed with ". $response->status_line; }