Groovy XmlSlurper access attribute value in root node

心已入冬 提交于 2019-12-08 15:42:40

问题


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.

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

I've also tried:

def cobertura = new XmlSlurper().parse("cobertura-coverage.xml")
def coverage = cobertura.coverage
println coverage.@lines-covered

And:

def cobertura = new XmlSlurper().parse("cobertura-coverage.xml")
println cobertura.@lines-covered

回答1:


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'


来源:https://stackoverflow.com/questions/22769166/groovy-xmlslurper-access-attribute-value-in-root-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!