Tuesday, March 13, 2007

Perl exits in XML::Simple module

I'm using the perl module XML::Simple on a boatload of XML files that apparently do not all validate. When this happens, the code just exits with an error:
Invalid name in entity [Ln: 3, Col: 15]

Problem is, I don't want the code to exit. I want the code to ignore the problematic XML file, and skip to the next one. Can this be done? I tried using the Error module, but it did not help. Sample code:

#!/usr/bin/perl

use strict;
use warnings;

use Error qw(:try);
use XML::Simple;

my $ii;
for ($ii = 0; $ii < 10; $ii++) {
try {
my $loc = "/tmp/test.xml";
my $xmldata = XMLin($loc);
print "got data\n";
} catch Error with {
print "problem\n";
my $ex = shift; # Get hold of the exception object
} finally {
print "finishing\n";
}
}

Here's the output:

wereldmuis@hostname:~$ perl testing.pl
finishing
Invalid name in entity [Ln: 3, Col: 15]

Bummer, the thing just gags on the first hit of the invalid xml file, displayed here:

<metadata>
<file name="hello.file">
Simon & Garfunkel
</file>
</metadata>

0 Comments:

Post a Comment

<< Home