Groovy xmlparser获取属性值

Groovy xmlparser获取属性值,groovy,Groovy,我无法从以下XML解析属性值: s='''<?xml version="1.0" encoding="UTF-8"?> <web-ext xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http:/

我无法从以下XML解析属性值:

s='''<?xml version="1.0" encoding="UTF-8"?>
<web-ext
  xmlns="http://websphere.ibm.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd"
  version="1.0">
  <reload-interval value="3"/>
  <context-root uri="foo/bar" />
  <enable-directory-browsing value="false"/>
  <enable-file-serving value="true"/>
  <enable-reloading value="true"/>
  <enable-serving-servlets-by-class-name value="false" />
</web-ext>
'''

def contextroot
def xml = new XmlParser(false,false).parseText(s)
xml.each {
 if (it.name() == "context-root")
 contextroot = it.attributes().uri
}

不起作用。

如果要直接访问此属性,可以使用

xml.'context-root'[0].@uri

或者确实是
xml.'context-root.@uri[0]
这才是真正的groovy:-)尝试了一些方法,但没有找到正确的语法。通常我使用XmlSlurper,但有一个异常,所以我使用XmlParser。但是XmlSlurper(false,false).parseText()也可以工作。嗯,好吧,那是星期五;-)谢谢@tim_yates谢谢,用了你的建议,感觉更“自然”。将只有一个上下文根节点。
xml.'context-root'[0].@uri