Forum Discussion

_lauren11's avatar
_lauren11
Occasional Contributor
5 years ago

How to fetch a value of the node when given the condition

From the given index.price in all the nodes, i am able to find out the index.price having maximum value. My query is, how to find out the value of index.price1, from the node which is having index.price as maximum
Here's the sample xml:--
<Results>
<ResultSet fetchSize="0">
<Row rowNumber="1">
<INDEX>86</INDEX>
<INDEX.PRICE>22999.0</INDEX.PRICE>
<INDEX.PRICE1>11999.0</INDEX.PRICE1>
</Row>
<Row rowNumber="2">
<INDEX>90</INDEX>
<INDEX.PRICE>2999.0</INDEX.PRICE>
<INDEX.PRICE1>1299.0</INDEX.PRICE1>
</Row>
<Row rowNumber="3">
<INDEX>92</INDEX>
<INDEX.PRICE>3399.0</INDEX.PRICE>
<INDEX.PRICE1>1399.0</INDEX.PRICE1>
</Row>
<Row rowNumber="4">
<INDEX>100</INDEX>
<INDEX.PRICE>3099.0</INDEX.PRICE>
<INDEX.PRICE1>1598.0</INDEX.PRICE1>
</Row>
<Row rowNumber="5">
<INDEX>116</INDEX>
<INDEX.PRICE>3399.0</INDEX.PRICE>
<INDEX.PRICE1>1399.0</INDEX.PRICE1>
</Row>
<Row rowNumber="6">
<INDEX>160</INDEX>
<INDEX.PRICE>3199.0</INDEX.PRICE>
<INDEX.PRICE1>1299.0</INDEX.PRICE1>
</Row>
<Row rowNumber="7">
<INDEX>161</INDEX>
<INDEX.PRICE>2899.0</INDEX.PRICE>
<INDEX.PRICE1>1499.0</INDEX.PRICE1>
</Row>
</ResultSet>
</Results>

i am using this query to find out the maximum value of index price. What can be done to find out it's  index.price1

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def response = context.expand( '${JDBC Request#ResponseAsXml}' )
log.info response
def v=groovyUtils.getXmlHolder("JDBC Request#ResponseAsXml")
def price=v.getNodeValues("//*:INDEX.PRICE")
log.info price.toString()
def maxim= price.max()//to find maximum out from the list
log.info maxim

 

 

  • _lauren11 : Yes now i get the better understanding. Please use below code,

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def response = context.expand( '${JDBC Request#ResponseAsXml}' )
    def v=groovyUtils.getXmlHolder(response)
    def price=v.getNodeValues("//Row")
    def desiredVal, maxVal
    List<Double> values = new ArrayList<Double>();
    for( item in v.getNodeValues( "//Row/INDEX.PRICE" )){
    	values.add( Double.parseDouble(item) );
    }
    maxVal =  values.max()
    
    for(int i = 0 ; i < price.size() ; i++){
    	if( v.getNodeValues( "//Row/INDEX.PRICE" )[i] == "$maxVal" ){
    		desiredVal = v.getNodeValues( "//Row/INDEX.PRICE1" )[i]
    	}
    }
    log.info desiredVal.toString()
    

     

    Hope it will solve your problem.

     

     

  • _lauren11 

     

    Here you go:

    //Pass the jdbc response xml into jdbcResult variable.
    
    log.info new XmlSlurper().parseText(jdbcResult).'**'.find {it.name() == 'Row' && it.'INDEX.PRICE'.max()}.'INDEX.PRICE1'.text()

     

    You can test it online here

    https://ideone.com/xXgmDh

  • nmrao's avatar
    nmrao
    Champion Level 3

    _lauren11 

     

    Here you go:

    //Pass the jdbc response xml into jdbcResult variable.
    
    log.info new XmlSlurper().parseText(jdbcResult).'**'.find {it.name() == 'Row' && it.'INDEX.PRICE'.max()}.'INDEX.PRICE1'.text()

     

    You can test it online here

    https://ideone.com/xXgmDh

  • Hope i am understanding it correctly. You want to find INDEX.PRICE1 instead of INDEX.PRICE

     

    so just replace 

     

    def price=v.getNodeValues("//*:INDEX.PRICE") with

    def price=v.getNodeValues("//*:INDEX.PRICE1")

     

    else is fine with your script.

     

    • _lauren11's avatar
      _lauren11
      Occasional Contributor

      Thanks for the reply. I am afraid, lemme rephrase my question:---

      My requirement is to find out the value of INDEX.PRICE1 for the one having INDEX.PRICE as maximum.

      eg. here, maximum value of INDEX.PRICE=1598. Now i want to print it's respective INDEX.PRICE1

       

      HimanshuTayal sir

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        _lauren11 : Yes now i get the better understanding. Please use below code,

         

        def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
        def response = context.expand( '${JDBC Request#ResponseAsXml}' )
        def v=groovyUtils.getXmlHolder(response)
        def price=v.getNodeValues("//Row")
        def desiredVal, maxVal
        List<Double> values = new ArrayList<Double>();
        for( item in v.getNodeValues( "//Row/INDEX.PRICE" )){
        	values.add( Double.parseDouble(item) );
        }
        maxVal =  values.max()
        
        for(int i = 0 ; i < price.size() ; i++){
        	if( v.getNodeValues( "//Row/INDEX.PRICE" )[i] == "$maxVal" ){
        		desiredVal = v.getNodeValues( "//Row/INDEX.PRICE1" )[i]
        	}
        }
        log.info desiredVal.toString()
        

         

        Hope it will solve your problem.