Hi,
The script below combines an XML Parse of a number of files in a directory, extracting the contents and inserting into a mysql database.
Whats happening is when an unrecognised encoding comes in XML::Parser bombs out as expected. The rest of the files are processed fine, the db inserts work, and the script ends with a nice messy segfault, though no core dump.
Am I not handling my die's correctly or is it something else ? I've currently 'handled' it by using LibXML instead!!
XML::Simple ( 1.08 ) Perl (5.6.1) XML::Parser (2.30) DBI (1.14), and DBD::mysql (2.0419).
use strict;
use DBI;
use XML::Simple;
use Data::Dumper;
my $dbh = DBI->connect( 'DBI:mysql:emailb:127.0.0.1', 'user', 'pass',
{ RaiseError => 1, PrintError => 1 } )
+;
die "db connect failed $dbh" unless (ref $dbh eq 'DBI::db');
my $xdir = '/var/tmp/xmlfeed/';
opendir( DIRH, $xdir) or die "cant open $xdir $!";
my @files = grep { /xml$/ } readdir DIRH;
closedir DIRH;
print "Total files : ", scalar @files, "\n";
my $inc = 0;
FILES : for (@files) {
my $xmlfile = $xdir . $_;
$inc++;
print "Processing file number : $inc : $xmlfile \n";
my $struct;
my $parser = new XML::Simple( forcearray => 1 );
eval {
$struct = $parser->XMLin( $xmlfile );
};
if ($@) { print "XML file : $xmlfile invalid ", $@; next; };
undef $@;
my $sth = $dbh->prepare(' insert into category_name values ( ?, ?
+)' );
for ( @{ $struct->{'Story'} } ) {
my $hashref = $_;
eval {
$sth->execute( @{ $hashref->{'cat_id'} } );
$sth->finish;
};
if ($@) {
print "category name insert failed : $@ : skipping \n", Du
+mper ($hashref ), "\n------------\n";
next;
}
undef $@;
}
$struct = undef;
}
$dbh->disconnect;