#!/usr/bin/perl -- #~ 2011-07-15-22:39:54 by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 # use lib grep -d, '/home/bcnagle/perl', ( getpwuid $> )[7].'/perl/'; use strict; use warnings; use XML::Twig; use DBI; Main( @ARGV ); exit( 0 ); sub Main { #~ DoTheDo( connect_to_db() , '/home/bcnagle/file.xml'); DoTheDo( connect_to_db(), DemoData() ); } BEGIN { our @Atts = ( 'path', 'Product_ID', 'Updated', 'Quality', 'Supplier_id', 'Prod_ID', 'Catid', 'On_Market', 'Model_Name', 'Product_View', 'HighPic', 'HighPicSize', 'HighPicWidth', 'HighPicHeight', 'Date_Added' ); sub DoTheDo { my( $dbh, $xmlFile ) = @_; my $insert = $dbh->prepare( sprintf 'INSERT INTO files (%s) VALUES (%s)', join( ',', map { $dbh->quote_identifier($_) } @Atts ), join( ',', map { '?' } 1 .. @Atts ), ); my $callback = sub { my( $twig, $file ) = @_; $insert->execute( map { '' . $file->att($_) } @Atts ); $twig->purge; }; my $twig = XML::Twig->new( #~ twig_handlers => { start_tag_handlers => { # if you only want without children 'filesindex/file' => $callback, } ); #~ $twig->parsefile( $xmlFile ); $twig->xparse( $xmlFile ); $dbh->disconnect(); } ## end sub DoTheDo sub connect_to_db { my $dbh = DBI->connect( 'dbi:SQLite:dbname=short.test.sqlite', undef, undef, { RaiseError => 1, PrintError => 1, }, ); my $sql = sprintf 'CREATE TABLE files ( %s )', join( ",\n", map { $dbh->quote_identifier($_)." TEXT " } @Atts ); print "\n$sql\n"; eval { $dbh->do( $sql ) } or warn $@; return $dbh; } ## end sub connect_to_db } ## end BEGIN sub DemoData { ## xml_pp -s record ## http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=914742;part=2 my $xml = <<'__XML__'; __XML__ return $xml } ## end sub DemoData __END__