BruceyBandit
BruceyBandit

Reputation: 4334

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.

<xxxxmlns:s="http://www.w3.org/xxx" xmlns:a="http://xxxg">
   <xxxxx>
      <xxx"></xxx>
   </xxxxx>
   <aaa>
      <abc xmlns="...">
         <bbb xmlns:b="..." xmlns:i="...">
            </bbb>
            <abc>   
               <aaa>
                  <b:HotelId>00000</b:HotelId>

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
}

Upvotes: 0

Views: 112

Answers (1)

Rao
Rao

Reputation: 21389

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
}

Upvotes: 3

Related Questions