So in XML::Parser in Stream and Subs mode they're definitely not per-element, so you'd need a gigantic switch-or-equivalent statement in your one-and-only StartTag routine to identify and instantiate the particular class you're after, plus it maintains no context (such as a parse stack) for you to tie into (I understand that this is a major reason for writing most of the other Perl XML parsers in fact, even though you can kind-of work around the problem using closures), plus it's not at all OO.

XML::Twig also doesn't seem to have a good solution, in that it instantiates all elements in the same class — a user-selected class to be sure, but always the same one.

I've discovered a name for what I'm after: XML Data Binding. Here's an article on the subject. It's Java-centric though, and doesn't offer any Perl answers...


In reply to Re^2: Parse XML to my own objects? by conrad
in thread Parse XML to my own objects? (aka XML Data Binding) by conrad

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.