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.


In reply to Re^3: Retrieving an XML node value with XML::DOM::Lite and XML::Parser by roboticus
in thread Retrieving an XML node value with XML::DOM::Lite and XML::Parser by young_monk_love_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.