xml - 节点名称中包含特殊字符的 Groovy XmlSlurper

标签 xml groovy

我正在尝试使用 groovy xml slurper 来获取以下 XML,目标是达到:

<message>ARoute</message> 

标签。

<org.kie.server.api.model.ServiceResponse>
<type>SUCCESS</type>
<msg>Container container2 successfully called.</msg>
<result class="string">
    <execution-results>
    <result identifier="helloWorld">
<com.me.testproject.HelloWorld>
  <message>ARoute</message>
</com.me.testproject.HelloWorld>
</result>
<fact-handle identifier="helloWorld" external-form="0:1:1985641387:1985641387:2:DEFAULT:NON_TRAIT:com.me.testproject.HelloWorld"/>
</execution-results></result>
</org.kie.server.api.model.ServiceResponse>

当我只使用以下 gpath 时:

def response = new XmlSlurper().parseText(payload)
def result = response.result
println( "Output is " + result)

我得到以下内容(我期望的)

Output is <execution-results>
 <result identifier="helloWorld">
<com.me.testproject.HelloWorld>
  <message>ARoute</message>
</com.me.testproject.HelloWorld>
</result>
<fact-handle identifier="helloWorld" external-  form="0:37:1288414012:1288414012:74:DEFAULT:NON_TRAIT:com.me.testproject.HelloWorld"/>
</execution-results>

但是,当我尝试解析执行结果时,有些东西似乎出现了问题,我的理解是由于 xml 节点名称中的连字符,我应该将其放入字符中,但这似乎不起作用:

def response = new XmlSlurper().parseText(payload)
def result = response.result.'execution-results'
println( "The type is " + result)

输出: 类型是

我该如何深入研究 XML 消息以获取消息标记?

最佳答案

默认情况下,gpath表达式的toString()相当于text()方法:它只显示表达式匹配的节点的文本内容。简而言之:表达式已经起作用,但是您的 println 没有显示您所期望的内容。

如果您希望内容为xml,可以使用groovy.xml.XmlUtil.serialize(result)

关于xml - 节点名称中包含特殊字符的 Groovy XmlSlurper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35038460/

相关文章:

xml - 使用 Oxygen 的 RDF 文档的验证错误

ajax - 在Grails中应用服务器端Javascript验证

groovy - 如何在gsp中使用replaceAll?

mysql - 在shell脚本中将字符串转换为xml?

javascript - 无法获取XML中的节点值,如何获取?

jquery - jQuery ajax 成功函数的额外参数

xml - 如何以特定的product.id 形式给出操作调用?

java - 压缩/缩小/优化 groovy-all、groovy 2.0.1 进行分发?

validation - Grails 重复错误消息

android-studio - Android Studio:任务 ':buildSrc:compileGroovy'的执行失败