I think it is going to be rather difficult to create the object while you parse the object tree. You have to have some kind of knowledge of what you are going to expect. At any rate, using a similar technique to the code i linked to, here is a script that parses the example XML from the link you provided. Hope it helps. :)
direct link to D/L code

package SalesOrder; use Class::MethodMaker new_hash_init => 'new', get_set => [qw(Customer OrderDate SONumber Items)], ; package Customer; use Class::MethodMaker new_hash_init => 'new', get_set => [qw(CustName CustNumber Street City State PostCode +)], ; package Item; use Class::MethodMaker new_hash_init => 'new', get_set => [qw(ItemNumber Part Quantity)], ; package Part; use Class::MethodMaker new_hash_init => 'new', get_set => [qw(PartNumber Description Price)], ; package main; use strict; use warnings; use Data::Dumper; use XML::Simple; my $xml = XMLin(\*DATA, KeyAttr => []); my $items = [ map Item->new(Part => Part->new(delete $_->{Part}), %$_) +, @{$xml->{Item}} ]; delete $xml->{Item}; my $sales_order = SalesOrder->new( Customer => Customer->new(%{ delete $xml->{Customer} }), Items => $items, %$xml, ); print Dumper $sales_order; __DATA__ <SalesOrder SONumber="12345"> <Customer CustNumber="543"> <CustName>ABC Industries</CustName> <Street>123 Main St.</Street> <City>Chicago</City> <State>IL</State> <PostCode>60609</PostCode> </Customer> <OrderDate>981215</OrderDate> <Item ItemNumber="1"> <Part PartNumber="123"> <Description>Turkey wrench: Stainless steel, one-piece constru +ction, lifetime guarantee.</Description> <Price>9.95</Price> </Part> <Quantity>10</Quantity> </Item> <Item ItemNumber="2"> <Part PartNumber="456"> <Description>Stuffing separator: Aluminum, one-year guarantee. +</Description> <Price>13.27</Price> </Part> <Quantity>5</Quantity> </Item> </SalesOrder>

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 Re^3: Parse XML to my own objects? by jeffa
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.