in reply to Re^2: XML::LibXML Parser Error
in thread XML::LibXML Parser Error

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; }

Replies are listed 'Best First'.
Re^4: XML::LibXML Parser Error
by omegaweaponZ (Beadle) on Mar 07, 2014 at 16:56 UTC
    I understand that. But it isn't failing the HTTP response...Output:
    HTTP response is good parser error : Document is empty parser error : Start tag expected, '<' not found
    The XML starts off properly with a <> tag everytime...

      So, check the content as well:

      my $xml = $response->content; if ($xml =~ /^</) { print "Looks like it could be xml/html\n"; # Parse xml here } else { die "Bad content:\n$xml\n"; }

      In fact, save the content on each invocation so you can see why it isn't parsing or what the difference is.

        So from initial testing, it seems that the parser will work fine when I write this to a temporary file, then read from that file, yet fail when reading directly from the web xml output. That seems completely counter intuitive. If I can read and write out proper xml everytime to a local file why can't the parser read and parse directly from the web xml output everytime!

        This still seems to suggest an error with the parser having issues from the web output even though the HTML returns correctly...

        However, what we can attempt to do is use this newly created string and parse as a string, instead of a location. That seems to bypass whatever issue has been causing problems, so we would use:

        $dom = XML::LibXML->new->load_xml(string => "$xml");
        Instead of the URL directly. This seems to do the trick.

        Thanks again for your help!