1AnyData::Format::XML(3)User Contributed Perl DocumentatioAnnyData::Format::XML(3)
2
3
4
6 AnyData::Format::XML - tiedhash and DBI access to XML
7
9 # access XML data via a multi-dimensional tied hash
10 # see AnyData.pod for full details
11 #
12 use AnyData;
13 my $table = adTie( 'XML', $file, $mode, $flags );
14
15 OR
16
17 # convert data to and from XML
18 # see AnyData.pod for full details
19 #
20 use AnyData;
21 adConvert( 'XML', $file1, $any_other_format, $file2, $flags );
22 adConvert( $any_other_format, $file1, 'XML', $file2, $flags );
23
24 OR
25
26 # access the data via DBI and SQL
27 # see DBD::AnyData.pod for full details
28 #
29 use DBI;
30 my $dbh = DBI->connect( 'dbi:AnyData' );
31 $dbh->func('mytable','XML',$file,$flags,'ad_catalog');
32
33 See below for a description of the optional flags that apply to all of
34 these examples.
35
37 This module allows you to create, search, modify and/or convert XML
38 data and files by treating them as databases without having to actually
39 create separate database files. The data can be accessed via a multi-
40 dimensional tiedhash using AnyData.pm or via DBI and SQL commands using
41 DBD::AnyData.pm. See those modules for complete details of usage.
42
43 The module is built on top of Michel Rodriguez's excellent XML::Twig
44 which means that the AnyData interfaces can now include information
45 from DTDs, be smarter about inferring data structure, reduce memory
46 consumption on huge files, and provide access to many powerful features
47 of XML::Twig and XML::Parser on which it is based.
48
49 Importing options allow you to import/access/modify XML of almost any
50 length or complexity. This includes the ability to access different
51 subtrees as separate or joined databases.
52
53 Exporting and converting options allow you to take data from almost any
54 source (a perl array, any DBI database, etc.) and output it as an XML
55 file. You can control the formating of the resulting XML either by
56 supplying a DTD listing things like nesting of tags and which columns
57 should be output as attributes and/or you can use XML::Twig
58 pretty_print settings to generate half a dozen different levels of
59 compactness or whitespace in how the XML looks.
60
61 The documentaion below outlines the special flags that can be used in
62 either of the interfaces to fine-tune how the XML is treated.
63
64 The flags listed below define the relationship between tags and
65 attributes in the XML document and columns in the resulting database.
66 In many cases, you can simply accept the defaults and the database will
67 be built automatically. However, you can also fine tune the generation
68 of the database by specifying which tags and attributes you are
69 interested in and their relationship with database columns.
70
72 Prerequisites
73 To use the tied hash interface, you will need
74
75 AnyData
76 XML::Twig
77 XML::Parser
78
79 To use the DBI/SQL interface, you will need those, and also
80
81 DBI
82 DBD::AnyData
83
84 Required flags ( none )
85 If no flags are specified, then the module determines the database
86 structure from examining the file or data itself, making use of the DTD
87 if there is one, otherwise scanning the first child of the XML tree for
88 structural information.
89
90 Optional flags
91 If the default behavior is not sufficient, you may either specify a
92 "record_tag" which will be used to define column names, or you can define an
93 entire tag-to-column mapping.
94
95 For simple XML, no flags are necessary:
96
97 <table>
98 <row row_id="1"><name>Joe</name><location>Seattle</location></row>
99 <row row_id="2"><name>Sue</name><location>Portland</location></row>
100 </table>
101
102 The record_tag will default to the first child, namely "row". The
103 column names will be generated from the attributes of the record tag
104 and all of the tags included under the record tag, so the column names
105 in this example will be "row_id","name","location".
106
107 If the record_tag is not the first child, you will need to specify it.
108 For example:
109
110 <db>
111 <table table_id="1">
112 <row row_id="1"><name>Joe</name><location>Seattle</location></row>
113 <row row_id="2"><name>Sue</name><location>Portland</location></row>
114 </table>
115 <table table_id="2">
116 <row row_id="1"><name>Bob</name><location>Boise</location></row>
117 <row row_id="2"><name>Bev</name><location>Billings</location></row>
118 </table>
119 </db>
120
121 In this case you will need to specify "row" as the record_tag since it
122 is not the first child of the tree. The column names will be generated
123 from the attributes of row's parent (if the parent is not the root),
124 from row's attributes and sub tags, i.e.
125 "table_id","row_id","name","location".
126
127 In some cases you will need to specify an entire tag-to-column mapping.
128 For example, if you want to use a different name for the database
129 column than is used in the XML (especially if the XML tag is not a
130 valid SQL column name). You'd also need to specify a mapping if there
131 are two tags with the same name in different places in the XML tree.
132
133 The column mapping is a reference to an array of column definitions. A
134 column definition is either a simple name of a tag, or a hash reference
135 with the key containing the full path of the XML tag and the value
136 containing the desired column name alias.
137
138 For example:
139
140 col_map => [ 'part_id', 'part_name', 'availability' ];
141
142 That will find the first three tags with those names and create the
143 database using the same names for the tags.
144
145 Or:
146
147 col_map => [
148 { '/parts/shop/id' => 'shop_id'},
149 { '/parts/shop/part/id' => 'part_id'},
150 { '/parts/shop/part/name' => 'part_name'},
151 ];
152
153 That would find the three tags referenced on the left and create a
154 database with the three column names referenced on the right.
155
156 When exporting XML, you can specify a DTD to control the output. For
157 example, if you import a table from CSV or from an Array, you can
158 output as XML and specify which of the columns become tags and which
159 become attributes and also specify the nesting of the tags in your DTD.
160
161 The XML format parser is built on top of Michel Rodriguez's excellent
162 XML::Twig which is itslef based on XML::Parser. Parameters to either
163 of those modules may be passed in the flags for adTie() and the other
164 commands including the "prettyPrint" flag to specify how the output XML
165 is displayed and things like ProtocolEncoding. ProtocolEncoding
166 defaults to 'ISO-8859-1', all other flags keep the defaults of
167 XML::Twig and XML::Parser. See the documentation of those modules for
168 details;
169
170 CAUTION: Unlike other formats, the XML format does not save changes to
171 the file as they are entered, but only saves the changes when you explicitly
172 request them to be saved with the adExport() command.
173
175 copyright 2000, Jeff Zucker <jeff@vpservices.com> all rights reserved
176
178 Hey! The above document had some coding errors, which are explained
179 below:
180
181 Around line 657:
182 =pod directives shouldn't be over one line long! Ignoring all 10
183 lines of content
184
185
186
187perl v5.12.0 2001-07-17 AnyData::Format::XML(3)