Prerequisites
To use the tied hash interface, you will need
AnyData
XML::Twig
XML::Parser
To use the DBI/SQL interface, you will need those, and also
DBI
DBD::AnyData
Requiredflags(none)
If no flags are specified, then the module determines the database structure from examining the file or
data itself, making use of the DTD if there is one, otherwise scanning the first child of the XML tree
for structural information.
Optionalflags
If the default behavior is not sufficient, you may either specify a
"record_tag" which will be used to define column names, or you can define an
entire tag-to-column mapping.
For simple XML, no flags are necessary:
<table>
<row row_id="1"><name>Joe</name><location>Seattle</location></row>
<row row_id="2"><name>Sue</name><location>Portland</location></row>
</table>
The record_tag will default to the first child, namely "row". The column names will be generated from
the attributes of the record tag and all of the tags included under the record tag, so the column names
in this example will be "row_id","name","location".
If the record_tag is not the first child, you will need to specify it. For example:
<db>
<table table_id="1">
<row row_id="1"><name>Joe</name><location>Seattle</location></row>
<row row_id="2"><name>Sue</name><location>Portland</location></row>
</table>
<table table_id="2">
<row row_id="1"><name>Bob</name><location>Boise</location></row>
<row row_id="2"><name>Bev</name><location>Billings</location></row>
</table>
</db>
In this case you will need to specify "row" as the record_tag since it is not the first child of the
tree. The column names will be generated from the attributes of row's parent (if the parent is not the
root), from row's attributes and sub tags, i.e. "table_id","row_id","name","location".
In some cases you will need to specify an entire tag-to-column mapping. For example, if you want to use
a different name for the database column than is used in the XML (especially if the XML tag is not a
valid SQL column name). You'd also need to specify a mapping if there are two tags with the same name in
different places in the XML tree.
The column mapping is a reference to an array of column definitions. A column definition is either a
simple name of a tag, or a hash reference with the key containing the full path of the XML tag and the
value containing the desired column name alias.
For example:
col_map => [ 'part_id', 'part_name', 'availability' ];
That will find the first three tags with those names and create the database using the same names for the
tags.
Or:
col_map => [
{ '/parts/shop/id' => 'shop_id'},
{ '/parts/shop/part/id' => 'part_id'},
{ '/parts/shop/part/name' => 'part_name'},
];
That would find the three tags referenced on the left and create a database with the three column names
referenced on the right.
When exporting XML, you can specify a DTD to control the output. For example, if you import a table from
CSV or from an Array, you can output as XML and specify which of the columns become tags and which become
attributes and also specify the nesting of the tags in your DTD.
The XML format parser is built on top of Michel Rodriguez's excellent XML::Twig which is itself based on
XML::Parser. Parameters to either of those modules may be passed in the flags for adTie() and the other
commands including the "prettyPrint" flag to specify how the output XML is displayed and things like
ProtocolEncoding. ProtocolEncoding defaults to 'ISO-8859-1', all other flags keep the defaults of
XML::Twig and XML::Parser. See the documentation of those modules for details;
CAUTION: Unlike other formats, the XML format does not save changes to
the file as they are entered, but only saves the changes when you explicitly
request them to be saved with the adExport() command.