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)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.