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


In reply to 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.