Database Reference
In-Depth Information
# If table name was given, use it to create query that selects entire table.
# Split on dots in case it's a qualified name, to quote parts separately.
$stmt = "SELECT * FROM " . $dbh -> quote_identifier ( split ( /\./ , $tbl_name ))
if defined ( $tbl_name );
# Issue query and write XML
$gen -> execute ( $stmt );
$dbh -> disconnect ();
Other languages might have library modules to perform similar XML export operations.
For example, the Ruby DBI::Utils::XMLFormatter module has a table method that
exports a query result as XML. Here's a simple script that uses it:
#!/usr/bin/ruby -w
# xmlformatter.rb: Demonstrate DBI::Utils::XMLFormatter.table method.
require "Cookbook"
stmt = "SELECT * FROM expt"
# override statement with command line argument if one was given
stmt = ARGV [ 0 ] if ARGV . length > 0
dbh = Cookbook . connect
DBI : :Utils :: XMLFormatter . table ( dbh . select_all ( stmt ))
dbh . disconnect
11.10. Importing XML into MySQL
Problem
You want to import an XML document into a MySQL table.
Solution
Set up an XML parser to read the document, then use the document records to construct
and execute INSERT statements.
Discussion
Importing an XML document depends on being able to parse the document and extract
record contents from it. How you do that depends on how the document is written. For
example, one format might represent column names and values as attributes of <col
umn> elements:
<?xml version="1.0" encoding="UTF-8"?>
<rowset>
<row>
Search WWH ::




Custom Search