Ahhh, i see that working with the dreaded DOM has already got you thinking like a Java coder who has never been introduced to java.util.Hashtable. ;) What i mean is that you are not being generic enough. Check out this code which uses XML::Simple:
use strict; use warnings; use DBI; use XML::Simple; my $dbh = DBI->connect( ... ); my $xml = XMLin(\*DATA, keyattr => 'document'); insert($_) for @{$xml->{document}}; sub insert { my $hash = shift; my $sth = $dbh->prepare( 'insert into bar (' . join(',', keys %$hash) . ') values (' . join(',',map '?',keys %$hash) . ')' ); $sth->execute(values %$hash); } __DATA__ <documents> <document> <stayonhost>1</stayonhost> <staybelow>1</staybelow> <maxdepth>2</maxdepth> <name>Swedish Foo</name> <home_url>http://www.foo.se/bar/</home_url> <category>International</category> </document> <document> <stayonhost>0</stayonhost> <staybelow>0</staybelow> <maxdepth>3</maxdepth> <name>Swedish Bar</name> <home_url>http://www.bar.se/baz/</home_url> <category>National</category> </document> </documents>
Piece of cake. I can't help but feel that i am reinventing a wheel with my insert() sub however ...

UPDATE: May 2
Ahhh, how about a version with DBI::Wrap?

use DBI::Wrap; use XML::Simple; my $dbh = DBI::Wrap->new( ... ); $dbh->table('hacker'); my $xml = XMLin(\*DATA, keyattr => 'document'); $dbh->insert(Values => $_) for @{$xml->{document}}; __DATA__ (insert data from above snippet)

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)

In reply to (jeffa) Re: Rolling in the sheets with XML by jeffa
in thread Rolling in the sheets with XML by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.