Reputation: 3131
I have the following XML structure:
<RECORD>
<TYPE ID="check_ping">
<HOSTS>
<HOST H="192.168.0.1" W="50,1%" C="100,1%" />
<HOST H="192.168.0.2" W="50,1%" C="70,1%" />
</HOSTS>
</TYPE>
<TYPE ID="check_ssh">
<HOSTS>
<HOST H="192.168.0.3" P="21"/>
<HOST H="192.168.0.4" P="21"/>
<HOST H="192.168.0.4" P="21"/>
</HOSTS>
</TYPE>
....
</RECORD>
I have also a Perl script that is supposed to read that XML, I am using XML::XPath module.
use XML::XPath;
#.....
my $xp = XML::XPath->new(filename => 'test.xml');
#RETRIEVE ALL HOSTS WITH CHECK_SSH
my $querysshh = '/RECORD/TYPE[@ID="check_ssh"]/HOSTS/HOST';
$nodeset = $xp->find($querysshh);
foreach $mynode ($nodeset->get_nodelist) {
#HOW DO I RETRIEVE ATTRIBUTES NOW?
}
#.....
How can I retrieve the attributes of those nodes? For example if I want to read H and P what do I need to do? I looked at the module documentation but I couldnt find any useful method.
Upvotes: 3
Views: 5025
Reputation: 9697
As Martin has pointed out, the getAttribute
method should work fine. You can find other available methods in XML::XPath::Node::Element documentation.
Upvotes: 2