in reply to Re^2: 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
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Retrieving an XML node value with XML::DOM::Lite and XML::Parser
by young_monk_love_perl (Novice) on Dec 19, 2013 at 18:49 UTC |