Database Reference
In-Depth Information
<column name="subject" value="Jane" />
<column name="test" value="A" />
<column name="score" value="47" />
</row>
<row>
<column name="subject" value="Jane" />
<column name="test" value="B />
<column name="score" value="50" />
</row>
</rowset>
Another format uses column names as element names and column values as the contents
of those elements:
<?xml version="1.0" encoding="UTF-8"?>
<rowset>
<row>
<subject>Jane</subject>
<test>A</test>
<score>47</score>
</row>
<row>
<subject>Jane</subject>
<test>B</test>
<score>50</score>
</row>
</rowset>
Due to the various structuring possibilities, it's necessary to make some assumptions
about the format you expect the XML document to have. For the example here, I assume
the second format just shown. One way to process this kind of document is to use the
XML::XPath module, which enables you to refer to elements within the document using
path expressions. For example, the path //row selects all the <row> elements under the
document root, and the path * selects all child elements of a given element. You can use
these paths with XML::XPath to obtain first a list of all the <row> elements, and then for
each row a list of all its columns.
The following script, xml_to_mysql.pl , takes three arguments:
% xml_to_mysql.pl db_name tbl_name xml_file
The filename argument indicates which document to import, and the database and table
name arguments indicate the table into which to import it. xml_to_mysql.pl processes
the command-line arguments, connects to MySQL, and processes the document:
#!/usr/bin/perl
# xml_to_mysql.pl: Read XML file into MySQL.
use strict ;
use warnings ;
Search WWH ::




Custom Search