Forum Discussion

ngoyal's avatar
ngoyal
Occasional Contributor
6 years ago

How to pick minimum value of an attribute from soap response and pick next node value based on it

From below response I am trying to pick minimum value of AmountAfterTax and also value of corresponding HotelCode and ChainCode.   Response XML -  <soap:Envelope xmlns:soap="http://schemas.xmls...
  • nmrao's avatar
    nmrao
    6 years ago
    //Using this expectedStayInfo  just for testing
    def expectedStayInfo = [AmountAfterTax: 144.06, HotelCode: '03062', ChainCode: 'ABC' ]
    
    def xml = new XmlSlurper().parseText(response)
    def stays = xml.'**'.findAll {it.name() == 'RoomStay'}
    def actualStayInfo = [AmountAfterTax: 0, HotelCode: null, ChainCode: null]
    
    def updateActualStayInfo = { node, rate  -> 
      def isUpdate = false
      if ((0 == actualStayInfo.AmountAfterTax) || (actualStayInfo.AmountAfterTax > rate)) { 
        isUpdate = true
      }
      if (isUpdate) {
        def info = node.'**'.find{'BasicPropertyInfo' == it.name()}
        actualStayInfo.AmountAfterTax = rate
        actualStayInfo.HotelCode = info.@HotelCode.text()
        actualStayInfo.ChainCode = info.@ChainCode.text()
      }
    }
    stays.each { stay -> def tempMin = stay.'**'.findAll{it.name() == 'Base'}*.@AmountAfterTax.collect{it.toBigDecimal()}.min() updateActualStayInfo(stay, tempMin) } //test if the actual value of xml is matching assert actualStayInfo == expectedStayInfo