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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|