Trying to parse XML yourself isn't only non-productive, it's boring and tedious. Creating on the fly objects, however, is fun and exciting. Why bother with boring and tedious when you jump to fun stuff?

For example, let's run your sample XML through XML::Simple. I'll use the built-in DATA filehandle, so if you run this be sure you include it:

use XML::Simple; use Data::Dumper; my $xml = XMLin(\*DATA); print Dumper $xml; __DATA__ <ObjectType> <AppObject>hello</AppObject> <AppObjectField>gender</AppObjectField> <valueTargetPair value="MALE" targetPo="Incoming 1" /> <valueTargetPair value="FEMALE" targetPo="Incoming 2" /> </ObjectType>
When run on a computer that has XML::Simple installed, you should see something like:
$VAR1 = { 'AppObject' => 'hello', 'valueTargetPair' => [ { 'value' => 'MALE', 'targetPo' => 'Incoming 1' }, { 'value' => 'FEMALE', 'targetPo' => 'Incoming 2' } ], 'AppObjectField' => 'gender' };
Now ... let's turn it into an object:
my $xml = XMLin(\*DATA); bless $xml, $xml->{AppObject}; warn unless ref $xml eq 'hello';
Done. ;) But don't be fooled. $xml is still a reference to an anonymous hash reference, it just also happens to "BE A" 'hello' object. Now, let's run tadman's code on this (with a couple of modifications), but this time i won't bother blessing it, since there is no reason to for this simple example:
my $xml = XMLin(\*DATA); foreach my $entry (@{$xml->{valueTargetPair}}) { print "value=", $entry->{value}, $/; print "targetPo=", $entry->{targetPo}, $/; }
This prints:
value=MALE targetPo=Incoming 1 value=FEMALE targetPo=Incoming 2
Hope this convinces you to stick with parsers for parsing XML. The work has not only already been done, it's been tried and tested. ;)

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 3Re: HoAoH by jeffa
in thread hashes, arrays, and references... oh my by regan

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.