in reply to Re: Retrieving an XML node value with XML::DOM::Lite and XML::Parser
in thread Retrieving an XML node value with XML::DOM::Lite and XML::Parser

The debug statement you suggested output some references like :

ARRAY(0x94b3910)

But when i try what Eliya suggested :

my $B = $doc->selectNodes("A/B")->[$cpt]->nodeValue(); my $D = $doc->selectNodes("A/C/D")->[$cpt]->nodeValue(); print "$B / $D \n";

I get :  Use of uninitialized value $B in concatenation (.) or string at line xxx and the same for $A for each iteration of the loop

Replies are listed 'Best First'.
Re^3: Retrieving an XML node value with XML::DOM::Lite and XML::Parser
by roboticus (Chancellor) on Dec 18, 2013 at 22:04 UTC

    young_monk_love_perl:

    OK, that 'ARRAY(0xXXXXXXX)' means that the selectNodes function is returning an array reference. I'm guessing (based on a cursory glance at XML::DOM::Lite that it's returning a list of objects. Since your Xpath statements are different, there's no reason to expect that both lists of nodes are the same length, yet your code assumes that. You'll get a similar error for every iteration where your lists aren't the same length.

    I don't know exactly what you're trying to do, but perhaps this will give you an idea on how to handle it:

    my @list1 = $doc->selectNodes("A/B"); my @list2 = $doc->selectNodes("A/C/D"); while (@list1 or @list2) { my ($B,$D) = ('-EMPTY-','-EMPTY-'); if (@list1) { my $t = shift @list1; $B = $t->nodeValue(); } if (@list2) { my $t = shift @list1; $D = $t->nodeValue(); } print "$B / $D \n"; }

    Since the lists aren't necessarily the same length, it may even better to simply process the lists independently:

    print "B NODES:\n"; print "\t", $_->nodeValue(), "\n" for $doc->selectNodes("A/B"); print "\nD NODES:\n"; print "\t", $_->nodeValue(), "\n" for $doc->selectNodes("A/C/D");

    The error in Eliyas version is another indication that your lists are different length. When the B list ran out of values, the $B variable was left undef because you ran off the end of the array. (Unless there was actually an undef in the list.)

    In either case, you need to verify that the functions are returning what you expect to see, and when they don't, perform some suitable action.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Lists always have the same length in my case. The uninitialized value error is display for each iteration of the loop from the first to the last ...

      But thank you for the structure it's still useful for me :) I'm trying it right now.

      And if there's an array reference different for each value at each iteration of the loop does that means that values are actually initialized ?