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", Dumper ($hashref ), "\n------------\n"; next; } undef $@; } $struct = undef; } $dbh->disconnect;