I'm reading some data using a DOM interface (XML::XPath, hoping for XML::XMLlib some day). It seems to be the way to go to be more than "simple", to give complete coverage to the feature set and it is a standard interface. However, it's not Perl. Rather than picking through the resulting Element and its related objects every time access is needed, I'm "deserializing" the data of interest into Perl data structures.

That is what XML::Simple does, but without sufficient guidance of what is wanted, and without all features being available. That is what .NET (excuse my French) does with the "Deserialize" attributes, and something that I've wrestled with because it's poorly designed and incomplete.

The point is, there is a step to do this transformation. And the work is mostly simple and repetitive. When coding, in an initial effort I decomposed the step into a function; e.g. take a DOM Node of type Element, and return a Foo structure (dumb structure, not a class). To eliminate duplicate code, I pushed the most common thing to do here into a separate function: take a list of names, and copy attributes having those names (if they exist) into a destination hash.

The idea is that a small number of to-the-point functions, with a reasonable amount of flexibility staying within its point, can be called to easily implement the function that converts a DOM Element into a Foo, for any Foo. That's as opposed to having a huge complex configuration structure that does it all automatically once set up, assuming the designer thought of everything. The provided functions can be used when applicable, with a small amount of code between them to handle odd things, and no problem convincing it to do something totally different.

So why isn't this already a nice CPAN module?

Has everyone written code like this? Or am I missing something?

—John


In reply to Reading structures from XML by John M. Dlugosz

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.