perl - Get value of elements using XML::DOM -
for life of me can't seem figure out.
i got below sample file:
<?xml version="1.0" encoding="utf-8"?> <datafile> <contract> <a>234</a> <b>234</b> <c>2fs4</c> </contract> <contract> <a>234</a> <b>21</b> <c>2fs4</c> </contract> <contract> <a>23v</a> <b>234</b> <e/> <c>2fs4</c> </contract> <contract> <a>234</a> <b>234</b> <e/> <c>2fs4</c> <d>234</d> </contract> <contract> <a>2343</a> <b>234</b> <e/> <c>2fs4</c> <d>3kv</d> </contract> </datafile>
i want element values each tag: value of a, 234,234,23v... , on.
this got far:
use xml::dom; $parser = new xml::dom::parser; $doc = $parser->parsefile ("./sample.xml"); $nodes = $doc->getelementsbytagname ("contract"); $n = $nodes->getlength; (my $i = 0; $i < $n; $i++) { $node = $nodes->item ($i); #returns 1 of contracts's array hash #print $node->getvalue . "\n"; #print $node; #print $node->tostring . "\n"; foreach $myelement ($node->getchildnodes){ #print $myelement . "\n"; #print $myelement->tostring . "\n"; #prints string each childnode #print $myelement->getnodename() . "\n"; #prints element tag print $myelement->getnodevalue() . "bla \n";#doesn't print thing } }
as can see idea pretty simple. getelementsbytagname return me list of contract. use loop step through list of contract. in each contract child nodes, a, b,c etc. stuck. tried several methods on xml::dom on search.cpan.org , keep getting hashed stuff, not actual text values.
i found example required xml::dom::lite, while works on laptop doesn't work on office computer since don't have sudo install xml::dom::lite. has xml::dom , xml::parser seems.
Comments
Post a Comment