How to find all XML elements by tag name in Groovy?

Viewed 48932

How I can find all elements in XML by their tag name in Groovy (GPath)?

I need to find all car elements in this document:

<records>
  <first>
    <car>
      <id>378932</id>
    </car>
  </first>
  <second>
    <foo>
      <car>
       <name>audi</name>
      </car>
    </foo>
  </second>
</records>

This is what I tried and failed:

def xml = new XmlSlurper().parse(file)
assert xml.car.size() == 2
3 Answers
Related