This does the trick...

use 5.010; use Data::Dumper; use XML::LibXML 1.70; my $xml = XML::LibXML->load_xml(IO => \*DATA); my @results; foreach my $dict ($xml->findnodes('//dict')) { my $key = $dict->getAttribute('key'); next unless $key ~~ [qw( subnet gateway cidr netmask )]; push @results, {} if $key eq 'subnet'; my $value = $dict->findvalue('.//value'); $results[-1]{ $key } = "$value"; } print Dumper \@results; __DATA__ <root> <dict key="status"> <value>success</value> </dict> <dict key="data"> <list> <dict key="subnet"> <value>10.11.14.0</value> </dict> <dict key="gateway"><value>10.11.13.1</value> </dict> <dict key="cidr"><value>10.11.14.0/21</value> </dict> <dict key="netmask"><value>255.255.248.0</value> </dict> <dict key="subnet"><value>10.11.15.0</value> </dict> <dict key="gateway"><value>10.11.13.1</value> </dict> <dict key="cidr"><value>10.11.15.0/24</value> </dict> <dict key="netmask"><value>255.255.255.0</value> </dict> <dict key="subnet"> <value>10.15.8.0</value> </dict> <dict key="gateway"> <value>10.11.13.1</value> </dict> <dict key="cidr"> <value>10.15.8.0/22</value> </dict> <dict key="netmask"> <value>255.255.252.0</value> </dict> <dict key="subnet"> <value>10.7.17.22</value> </dict> <dict key="gateway"> <value>10.11.13.1</value> </dict> <dict key="cidr"> <value>10.7.17.224/28</value> </dict> <dict key="netmask"> <value>255.255.255.240</value> </dict> <dict key="subnet"> <value>10.11.24.0</value> </dict> <dict key="gateway"> <value>10.11.13.1</value> </dict><dict key="cidr"> <value>10.11.24.0/24</value> </dict><dict key="netmask" ><value>255.255.255.0</value> </dict> </list> </dict> <dict key="exitCode"> <value>0</value> </dict> </root>
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Struggling with XML by tobyink
in thread Struggling with XML 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.