Personally, i think you shouldn't concern yourself with
incrementing XML tags (even though i played along and
posted a solution myself). The first
<foo> tag encountered is the first, and the
second <foo> tag encountered is the second, and
so on - as long as that's how you intend to read the data.
XML::Simple handles this by exposing an option
(forcearray). Since order will be preserved, why
bother with incrementing the tags? (And besides, wouldn't
that id number be better as an attribute instead?)
I posted a few solutions to converting CSV to XML over at
CSV to XML (the quick and dirty way). Ultimately, CSV::XML looks the
easiest, but i still dig using XML::Generator::DBI.
One option i didn't try at the time, however, was
DBD::AnyData. Here's one that follows the
<colN> naming convention and uses the
previous two modules ... but, caveats:
- commas are used instead of semi-colons -
DBD::AnyData does not handle them, but could be
written to do so, if desired. However, note that CSV stands
for comma. ;)
- ughh ... i have to specify the maximum number of columns
to be expected. This is not something i advocate, but then
again, this is all because the <colN>
have to be incremented.
Enough, here's the code:
use strict;
use warnings;
use DBI;
use XML::Generator::DBI;
use XML::Handler::YAWriter;
my $max = 5;
my $data = join(',',map"col$_",1..$max) . do {local $/;<DATA>};
my $dbh = DBI->connect('dbi:AnyData(RaiseError=>1):');
$dbh->func('test', 'CSV', [$data], 'ad_import');
my $generator = XML::Generator::DBI->new(
Handler => XML::Handler::YAWriter->new(AsFile => '-'),
dbh => $dbh,
Indent => 1,
);
$generator->execute('select * from test');
__DATA__
colContents,nextColContents,lastColContents
colContents2,nextColContents2,lastColContents2
colContents3,nextColContents3,lastColContents3
a,b,c,d,e
f,g,h,i
j,k
l
m,n,o
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] |