1

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)?

saravana_pc
  • 2,607
  • 11
  • 42
  • 66

3 Answers3

1

Assuming you have the xml in a String xml, you should be able to do:

List prices = new XmlSlurper().parseText( xml ).car.price*.text()​​
tim_yates
  • 167,322
  • 27
  • 342
  • 338
1

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 }
saravana_pc
  • 2,607
  • 11
  • 42
  • 66
0

May I suggest you next variant:

def vehicle = new XmlSlurper().parseText(xmlString)
vehicle.car.price.each {println "car's price:"+it}
olyv
  • 3,699
  • 5
  • 37
  • 67