I'm parsing some generic XML from an application's "templates", and serving it out in a different format and also storing it in a database, and I'd like to triple-check my logic here. I'm not that good with the XML::Foo modules yet (like XML::DOM, XML::Parser, XML::RSS::Tools, etc.) but I'm getting there.

My input data looks like this:

<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> [... up to 40 other element pairs..] </document>

Here's what I have thus far (featuring the snazzy new 2003 floor model PM indenting!):

use strict; # er, um, I forgot use DBI; # for storing in SQL use XML::DOM; # roll through the nodes # XML pre-requisites my $file = "funkyapp.xml"; my $xp = new XML::DOM::Parser(); my $doc = $xp->parsefile($file); my $root = $doc->getDocumentElement(); my @nodes = $root->getChildNodes(); foreach my $node (@nodes) { # get child nodes (yes, "childs") if ($node->getNodeType() == 1) { # check element name foreach my $item (@childs) { if ($node->getNodeType() == 1) { my @childs = $node->getChildNodes(); # iterate through child nodes foreach my $item (@childs) { # check element name ################################################# if (lc($item->getNodeName) eq "name") { my $name = $item->getFirstChild()->getData; ################################################# } elsif (lc($item->getNodeName) eq "home_url") { my $url = $item->getFirstChild()->getData; ################################################# } elsif (lc($item->getNodeName) eq "stayonhost") { my $stayhost = $item->getFirstChild()->getData; ################################################# } elsif (lc($item->getNodeName) eq "staybelow") { my $staybelow = $item->getFirstChild()->getData; ################################################# } elsif (lc($item->getNodeName) eq "maxdepth") { my $maxdepth = $item->getFirstChild()->getData; } # elsif { # [... 40 other nodes.. ] } # Insert values into SQL here, this works } }

This code works, and prints/stores the values I expect in the database, but it gets very repititious trying to add new nodes, and duplicating the code over and over.

The issue I'd like to solve here is that this input file could have about 40 different nodeName values. I'd like to eliminate the repetition of the code seen above to do this. Is there a simpler way to do that, and still give me the ability to print/store the values by name?

Update: added a description of my input data (thanks jeffa for the reminder)


In reply to 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.