parsing XML result by using perl?

for some reasons, i need to parse the XML result by using perl.
for instance, this is a sample XML result:

<Response>
<status>success</status>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</Response>

then i can use this way :

my @output = ();
foreach my $parts (@all)    ##@all = above XML result
{
  chomp($parts);

  if ($parts =~ /\<answer\>(.*)\<\/answer\>/)
  {
     push @output, $1;
  }
}

Then i can get a list for all the answers.
BUT i have problem in processing multiple sections in XML result, something like :

<Response>
<status>success</status>
<item key=1>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</item>
<item key=2>
<answer>CCC::CCC</answer>
<answer>DDD::DDD</answer>
</item>
</Response>

any advice on this? or is there any existing module for this parsing?
Thanks.

Take a look at XML::XPath module.

$
$
$ cat f67.xml
<Response>
<status>success</status>
<item key=1>
<answer>AAA::AAA</answer>
<answer>BBB::BBB</answer>
</item>
<item key=2>
<answer>CCC::CCC</answer>
<answer>DDD::DDD</answer>
</item>
</Response>
$
$
$ perl -lne 'if (/\<item key=(.*?)\>/) { $key = $1 }
             elsif (/\<answer\>(.*?)\<\/answer\>/) { push @{$x{$key}}, $1 }
             END {
               while ( ($k, $v) = each %x) {
                 print "Key = $k";
                 foreach $i (@$v) { print "\tAnswer = $i" }
               }
             }
            ' f67.xml
Key = 1
        Answer = AAA::AAA
        Answer = BBB::BBB
Key = 2
        Answer = CCC::CCC
        Answer = DDD::DDD
$
$
$

tyler_durden