views:

166

answers:

1

When I try to read an XML file with XML::Simple, sometimes I get this error message:

Couldn't create file parser context for file ...

After some googling, it seems to be a problem with libxml-libxml-perl and is supposed to be fixed in the version I use (1.59-2).

Any ideas?

Edit: (code)

sub Read
{
  my ($file, $no_option) = @_;
  my %XML_INPUT_OPTIONS = ( KeyAttr => [], ForceArray => 1 );

  if ((defined $file) && (-f $file))
  {
    my @stats = stat($file);
    if ((defined $XML_CACHE{$file})
      && ($stats[9] == $XML_CACHE{$file}{modif_time}))
    {
      return ($XML_CACHE{$file}{xml});
    }
    else
    {
      my $xml = eval { XMLin($file,
        (defined $no_option ? () : %XML_INPUT_OPTIONS)) };
      AAT::Syslog("AAT::XML", "XML_READ_ERROR", $@) if ($@);
      $XML_CACHE{$file}{modif_time} = $stats[9];
      $XML_CACHE{$file}{xml} = $xml;
      return ($xml);
    }
  }

  return (undef);
}

And yes, I should & will use XML::Simple cache feature...

+1  A: 

Does the error continue "No such file or directory at..."? If so, then I think that the problem is that (for whatever reason) when you get to that point in the script, whatever you are passing to XML::Simple has no xml file in it. Long story short, the script you are using may be passing a bad variable (blank? empty?) to XML::Simple at which point the module chokes. To debug, add a check on whatever you hand to XML::Simple before you pass it along. (See the next paragraph for a concrete example explaining why I think this may be your problem.)

A few months ago, I had a similar problem with Weather::Google. In a nutshell, the weather module was trying to get data from Google via LWP::Simple without a user agent. Google began (apparently) to reject requests without a user agent. I had to backtrack through the modules because the error appeared to come from XML::Simple. In fact, it was caused by what was done in LWP::Simple and Weather::Google. Or rather, the error was a result of Weather::Google not checking the data that was in an object created via LWP::Simple. In a case like this, it can be hard at first to see what's going wrong and where.

Telemachus
Yes, there is "No such file or directory at..." after, but I check that this file exists before XMLin (-f $file)
sebthebert