根节点中的Groovy XmlSlurper访问属性值 [英] Groovy XmlSlurper access attribute value in root node
本文介绍了根节点中的Groovy XmlSlurper访问属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Groovy和XmlSlurper完成对属于根节点的属性的访问.我可以使用嵌套节点轻松地做到这一点,但似乎无法访问根节点.
I'm trying to accomplish accessing the attributes that are part of the root node using Groovy and XmlSlurper. I can do this easily with nested nodes, but can't seem to access the root node.
这是XML结构(简化):
Here is the XML structure (simplified):
<coverage lines-covered="2353" lines-valid="2943">
<sources />
<packages />
</coverage>
我希望能够获得行覆盖的和行有效的属性值.这是我正在尝试的代码:
I'd like to be able to get to the lines-covered and lines-valid attribute values. Here is the code I'm trying out:
def cobertura = new XmlSlurper().parse(xml)
def coverage = cobertura.coverage
def lines = cobertura.find { it.@lines-covered }
println lines
我也尝试过:
def cobertura = new XmlSlurper().parse("cobertura-coverage.xml")
def coverage = cobertura.coverage
println coverage.@lines-covered
并且:
def cobertura = new XmlSlurper().parse("cobertura-coverage.xml")
println cobertura.@lines-covered
推荐答案
您需要在lines-covered
部分加引号,因为它包含破折号:
You need to put the lines-covered
part in quotes since it contains a dash:
def cobertura = new XmlSlurper().parse("cobertura-coverage.xml")
println cobertura.@'lines-covered'
这篇关于根节点中的Groovy XmlSlurper访问属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文