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"; }

In reply to XML::XPath - node-to-xpath reverse lookup by georgeh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.