http://www.perlmonks.org?node_id=1067659

young_monk_love_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to parse an XML file retrieve the value of specific nodes. There's the XML structure :

<A > <B>B value</B> <C> <D d_attribute"d_attribute_value">D_Value</D> </C> </A > <A > <B>B value</B> <C> <D d_attribute"d_attribute_value">D_Value</D> </C> </A > . . .

Here's my code :

use strict; use Switch; use warnings; use XML::Parser; use XML::DOM::Lite qw(Parser :constants); my $parser = Parser->new(); my $doc; my $xml_path; $doc = $parser->parseFile($xml_path, whitespace => 'strip'); my $A_list = $doc->getElementsByTagName('A'); if(defined($A_list)){ my $length = scalar(@$A_list); for (my $cpt = 0; $cpt < $length; $cpt++) { my $B = $doc->selectNodes("A/B")->index($cpt)->nodeValue(); my $D = $doc->selectNodes("A/C/D")->index($cpt)->nodeValue(); print "- $B / $D\n"; } print "\n\n"; }

But I get an error : Can't call method "index" on unblessed reference.

I'm probably doing it the wrong way. Could you please advise me ? Additionally I would like to retrieve the attribute value.

Thank you for your help, and please be tolerant, I'm just here to seek for your wisdom, I'm still learning Perl ...

Regards Alex