使用XmlSlurper检索顶级XML节点名称

时间:2013-10-17 23:17:48

标签: groovy xmlslurper

鉴于以下XML,我如何使用Groovy的XmlSlurper获取status的值:

<response status="success">
  <child>
     <age>21</age>
     <name>Jane McCoy</name>
  </child>
</response>

1 个答案:

答案 0 :(得分:2)

def xmlStr = """
<response status="success">
  <child>
     <age>21</age>
     <name>Jane McCoy</name>
  </child>
</response>
"""

def parsed = new XmlSlurper().parseText(xmlStr)
assert parsed.'@status' == 'success'