不支持直接导入!请参考《O'Reilly: Mysql Cookbook》中的内容: 10.42 Exporting Query Results as XML 10.43 Importing XML into MySQL以下附上书中的脚本:xml_to_mysql.pl#! /usr/bin/perl -w # xml_to_mysql.pl - read XML file into MySQLuse strict; use DBI; use XML::XPath;# ... process command-line options (not shown) ...# ... connect to database (not shown) ...# Open file for reading my $xp = XML::XPath->new (filename => $file_name); my $row_list = $xp->find ("//row"); # find set of <row> elements print "Number of records: " . $row_list->size ( ) . "\n"; foreach my $row ($row_list->get_nodelist ( )) # loop through rows { my @name; # array for column names my @val; # array for column values my $col_list = $row->find ("*"); # children (columns) of row foreach my $col ($col_list->get_nodelist ( )) # loop through columns { # save column name and value push (@name, $col->getName ( )); push (@val, $col->string_value ( )); } # construct INSERT statement, then execute it my $stmt = "INSERT INTO $tbl_name (" . join (",", @name) . ") VALUES (" . join (",", ("?") x scalar (@val)) . ")"; $dbh->do ($stmt, undef, @val); }$dbh->disconnect ();
10.42 Exporting Query Results as XML
10.43 Importing XML into MySQL以下附上书中的脚本:xml_to_mysql.pl#! /usr/bin/perl -w
# xml_to_mysql.pl - read XML file into MySQLuse strict;
use DBI;
use XML::XPath;# ... process command-line options (not shown) ...# ... connect to database (not shown) ...# Open file for reading
my $xp = XML::XPath->new (filename => $file_name);
my $row_list = $xp->find ("//row"); # find set of <row> elements
print "Number of records: " . $row_list->size ( ) . "\n";
foreach my $row ($row_list->get_nodelist ( )) # loop through rows
{
my @name; # array for column names
my @val; # array for column values
my $col_list = $row->find ("*"); # children (columns) of row
foreach my $col ($col_list->get_nodelist ( )) # loop through columns
{
# save column name and value
push (@name, $col->getName ( ));
push (@val, $col->string_value ( ));
}
# construct INSERT statement, then execute it
my $stmt = "INSERT INTO $tbl_name ("
. join (",", @name)
. ") VALUES ("
. join (",", ("?") x scalar (@val))
. ")";
$dbh->do ($stmt, undef, @val);
}$dbh->disconnect ();