XML::Simple may work great for simple stuff, and though your new requirement may still be simple, it seems like it won't take long for you to surpass that. Personally, I find XML::Twig simple enough for the simple stuff, and no more complex for the complex stuff... so I'd recommend going that way sooner rather than later.

That said, this doesn't appear to be valid XML: there is no close tag for 'report'. So it seems unfortunate that XML::Simple doesn't complain (it's not really XML anymore). Anyway, if I just close that tag immediately, I get:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(); $twig->parse(\*DATA); for my $node ($twig->get_xpath('/root/node')) { print "Machine: ", $node->att('mid'), "\n"; my ($values) = $node->get_xpath('values'); print "\tvalues: ", $values->text(), "\n"; my ($time) = $node->get_xpath('time'); print "\ttime: ", $time->text(), "\n"; } __END__ <root> <report id="0" ip="1.1.1.1" /> <node mid="machine1" name="Winxp"> <values>1, 2, 3, 4, 5, 6</values> <time>110, 120, 130, 140, 150, 160</time> </node> <node mid="machine2" name="Win2003"> <values>1, 2, 3, 4, 5, 6</values> <time>110, 120, 130, 140, 150, 160</time> </node> </root>
And that gives:
Machine: machine1 values: 1, 2, 3, 4, 5, 6 time: 110, 120, 130, 140, 150, 160 Machine: machine2 values: 1, 2, 3, 4, 5, 6 time: 110, 120, 130, 140, 150, 160
If you close the report after the last node, the $twig->get_xpath will have to change to /root/report/node, or you can just use //node which will work either way.


In reply to Re: Parsing XML file for more than 1 child element with attributes (XML::Twig) by Tanktalus
in thread Parsing XML file for more than 1 child element with attributes by Anonymous Monk

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.