Search code examples
groovyxmlslurper

Groovy - XmlSlurper - find innermost element


I have the following xml:

<vehicle>
  <car>
    <price>100</price>
    <price>200</price>
  </car>
  <car>
    <price>300</price>
    <price>400</price>
  </car>
</vehicle>

Given an xml, how can we get the innermost elements (in this case, all the<price> elements)?


Solution

  • thanks Tim for the answer. I just figured out the following works too. And is more generic:

    def document = slurper.parseText(xml)
    def prices = document.'**'.findAll { it.children().size() == 0 }