How To Load An XML File Into MySQL Using Perl
erics, Posted August 3rd, 2011 at 8:18:05pm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/usr/bin/perl -w use XML::Simple; use Data::Dumper; # used for debugging the XML use DBI; use DBIx::SQLEngine; use strict; # Use KeyAttr to massage the xml into a proper hash my $xs = XML::Simple->new(KeyAttr => { value => 'type' }, forcearray => ['value']); my $parsed; eval { $parsed = $xs->XMLin($xml); 1; } or do { die "Error loading xml: $@"; }; #print Dumper $parsed; # DEBUG to see parsed XML my $dsn = join(':', qw( DBI mysql {DATABASE_NAME} localhost 3306 ) ) . ':'; my $dbh = DBIx::SQLEngine->new($dsn,'{MYSQL_LOGIN}','{PASSWORD}') || die "Database connection Error: $DBI::errstr\n"; my $rows = $parsed->{'item'}; eval { $dbh->do_bulk_insert( 'table' => 'myTable', 'values' => $rows, ); 1; } or do { die qq|SQL do_bulk_insert Crashed: $@|; }; exit 0; |
Leave Your Comment
All fields marked with "*" are required.