I would like your help in the following :
given the .xml file :
<network>
<netelement>
<node pwd="KOR-ASBG" func="describe_SBG_TGC">
<collection category="IMT" dir="Stream_statistics"></collection>
</node>
</netelement>
<netelement>
<node pwd="ADR-ASBG" func="describe_SBG_TGC">
<collection category="IMT" dir="Stream_statistics"></collection>
<collection category="IMT" dir="Proxy_registrar_statistics_ACCESS"></collection>
</node>
</netelement></network>
What I would like to do is to get the element with the attribute “KOR-ASBG”, for example,
but using only XPath.
I have written the following Perl code :
#!/usr/bin/perl -w
use strict ;
use warnings ;
use XML::LibXML ;
use Data::Dump qw(dump) ;
my $dump = "/some_path/_NETELEMENT_.xml" ;
my $parser = new XML::LibXML ; my $doc ;
eval{ $doc = $parser->parse_file($dump) ; } ;
if( !$doc ) { print "failed to parse $dump" ; next ; }
my $root = $doc->getDocumentElement ;
my $_demo = $root->find('/network/netelement/node[@pwd="KOR-ASBG"]') ;
print dump($_demo)."\n" ;
But, what it gets dispalyed is :
bless([bless(do{\(my $o = 155172440)}, "XML::LibXML::Element")], "XML::LibXML::NodeList")
So the question would be, how can I get the XML Element that contains the “pwd” attribute (that equals “KOR-ASBG”), using XPath ?
Thank you 🙂
PS. I have also tried :
my @_demo = $root->findnodes('/network/netelement/node[@pwd="KOR-ASBG"]') ;
print dump(@_demo)."\n" ;
and what it gets displayed is :
bless(do{\(my $o = 179552448)}, "XML::LibXML::Element")
Your dumper object is not lying to you; you are getting a node list. To access it you may either iterate through it or just access the first node:
Of course, all DOM methods are available to you once you get the actual node: