Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found