If you want to query your XML-file, XPath is the way: And here is an example (I slightly changed your XML-file so all elements have different content):
use strict; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new( xml => *DATA ); print 'There are ', $xp->find('queries/query')->size, " 'query' nodes. +\n"; foreach my $element (qw/topN layer filter name datatype/) { print "$element: ", ( join ', ', map { XML::XPath::XMLParser::as_string($_) =~ />([^<]*)</ } $xp->find("queries/query/$element")->get_nodelist ), "\n"; } ## end foreach my $element (qw/topN layer filter name datatype/) __DATA__ <?xml version='1.0'?> <queries> <query> <name>topHosts 20</name> <layer>LINK 20</layer> <topN>20</topN> <datatype>topHosts 20</datatype> <filter></filter> </query> <query> <name>topHosts 120</name> <layer>LINK 120</layer> <topN>120</topN> <datatype>topHosts 120</datatype> <filter></filter> </query> </queries>
Output:
There are 2 'query' nodes. topN: 20, 120 layer: LINK 20, LINK 120 filter: name: topHosts 20, topHosts 120 datatype: topHosts 20, topHosts 120
And yes I know that the regex to extract the text-data from the nodes is very crude ...

Update: added a code example.

Update2: simplified and shortened code

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: XML parsing problem by CountZero
in thread XML parsing problem by avi_2009

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.