BruceyBandit BruceyBandit - 7 months ago 85
Groovy Question

how to find an xml tag using groovy

I want to perform a basic if statement where if the xml tag

b:HotelId
is not displayed then log.info true, else log.info false, however I keep getting true displayed.

How can i correct the if statement so that if the tag is not displayed, it will output true?

Below is my if statement:

if (xml.'**'.any { it.name() != 'b:HotelId' })
{
log.info true
}
else
{
log.info false
}

Rao Rao
Answer Source

Here you go, follow in line comments.

//Find if there is such element, HotelId, in the xml 
def hotelId = xml.'**'.find{ it.name() == 'HotelId' }
//The size should be at least 1, so you want to print false
if (hotelId.size()) {
  log.info 'element found'
  log.info false
} else {
  //you want to print true
  log.info 'element not found'
  log.info true
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download