http://www.perlmonks.org?node_id=870027

georgeh has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use XML::XPath to extract interesting information from the tomcat url:

   /manager/status?XML=true

The interesting part of the output is:

<status>
  <connector name="http-0">
    <threadInfo currentThreadCount="0" currentThreadsBusy="0" maxThreads="200"/>
    <requestInfo bytesReceived="0" bytesSent="0" errorCount="0" maxTime="0" processingTime="0" requestCount="0"/>
    <workers></workers>
  </connector>
  <connector name="http-8080">
    <threadInfo currentThreadCount="158" currentThreadsBusy="10" maxThreads="200"/>
    <requestInfo bytesReceived="297" bytesSent="19350704517" errorCount="192504" maxTime="249349" processingTime="2242513592" requestCount="983650"/>
    <workers>
    </workers>
  </connector>
</status>

XML::XPath lets me find 'currentThreadsBusy' using an XPath construct like this:

   /status/connector/threadInfo/@currentThreadsBusy

The 'problem' is that the get_nodelist() method provides a list of nodes, which is OK, but I would really like to know the XPath to the specific nodes it finds. In this case, there are 2x 'connector' nodes, but there is no obvious way to figure out which 'connector' node a node belonged to, or even that it was the 'connector' nodes that were repeated (eg. it might have been 'threadInfo' ).

What I would really like is a way to query each node returned by get_nodelist(), and give me an XPath like this:

   /status[1]/connector[2]/threadInfo[1]/@currentThreadsBusy

This would uniquely identify the node where the data came from.

Q1. Is there some way XML::XPath will give me this information?

Q2. Is there another package (XML::Smart?) that will do this?

Sample code for testing:

#!/usr/bin/perl use strict; use XML::XPath; my $xml = ' <status> <connector name="http-0"> <threadInfo currentThreadCount="0" currentThreadsBusy="0" maxThrea +ds="200"/> <requestInfo bytesReceived="0" bytesSent="0" errorCount="0" maxTim +e="0" processingTime="0" requestCount="0"/> <workers></workers> </connector> <connector name="http-8080"> <threadInfo currentThreadCount="158" currentThreadsBusy="10" maxTh +reads="200"/> <requestInfo bytesReceived="297" bytesSent="19350704517" errorCoun +t="192504" maxTime="249349" processingTime="2242513592" requestCount= +"983650"/> <workers> </workers> </connector> </status> '; my $path = '/status/connector/threadInfo/@currentThreadsBusy'; my $xpath = XML::XPath->new( xml => $xml ); my $nodeset = $xpath->find($path); foreach my $node ($nodeset->get_nodelist) { print "FOUND: ", $xpath->getNodeText($node), "\n"; }