Groovy - XmlSlurper - 查找最内层的元素 [英] Groovy - XmlSlurper - find innermost element

查看:127
本文介绍了Groovy - XmlSlurper - 查找最内层的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml:

I have the following xml:

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

给定一个xml,我们如何获得最内层的元素(在这种情况下,所有的< price> elements)?

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

推荐答案

我只是想出了以下的作品。并且更通用:

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 }

这篇关于Groovy - XmlSlurper - 查找最内层的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆