如何在Groovy中通过标签名称查找所有XML元素? [英] How to find all XML elements by tag name in Groovy?
本文介绍了如何在Groovy中通过标签名称查找所有XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要找到所有 car
How I can find all elements in XML by their tag name in Groovy (GPath)?
如何通过Groovy(GPath)中的标签名称找到XML中的所有元素?
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
推荐答案
这是如何工作的:
This is how it works:
def xml = new XmlSlurper().parse(file)
def cars = xml.depthFirst().findAll { it.name() == 'car' }
assert cars.size() == 2
这篇关于如何在Groovy中通过标签名称查找所有XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文