snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

Even i have multiple records in xml file ,but below code only retrieve top record and not the other records ....
use strict; use warnings; use XML::XPath; use Data::Dumper; my @records; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $nodeset = $xp->findnodes('/application_list'); foreach my $node ($nodeset->get_nodelist) { my $appid = $xp->find('./application/@id', $node); s/^\s+|\s+$//g for $appid; push @records, { appid => $appid->string_value, } } print Dumper @records;

---Output

C:\SLB\Dashboard\Perl>perl testperl.pl $VAR1 = { 'appid' => '212' };

xml file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <application_list> <application id="212" ></application> <application id="565" ></application> <application id="654" ></application> <application id="975" ></application> </application_list>

Replies are listed 'Best First'.
Re: not fetching correct records in xml output
by hippo (Archbishop) on Jul 10, 2017 at 10:27 UTC

    You have not iterated over the nodeset returned by $xp->find so you only get one value back. See the docs for XML::XPath for an example of how to do that (it's right in the synopsis so I'm surprised you missed it).

    And if you want to dump an array, pass the reference to Dumper:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @records = qw/a b c/; print "Array: " . Dumper @records; print "Array ref: " . Dumper \@records;
Re: not fetching correct records in xml output
by poj (Abbot) on Jul 10, 2017 at 10:35 UTC
    for ( $xp->findnodes('/application_list/application/@id') ){ my $id = $_->string_value; $id =~ s/^\s+|\s+$//g; push @records, { appid => $id }; };
    poj
      Thanks for the inputs !!!