我正在Groovy中解析一个XML文件,稍后我需要追加返回的变量。
def lmList = slurperResponse.LastGlobal.HeatMap
String appendedString = lmList.country + lmList.ResponseTime + lmList.timeset这不适用于追加这3个字符串。它只是将第一个字符串分配到右边。如何在Groovy中正确地实现它?我尝试了concat,它抛出了以下错误:
groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.concat() is applicable for argument types: (groovy.util.slurpersupport.NodeChildren) values: [4468]
Possible solutions: toFloat(), collect(), collect(groovy.lang.Closure)
at 发布于 2016-07-14 20:39:05
作为注射器的替代物-
def lmList = slurperResponse.LastGlobal.HeatMap
String appendedString = lmList.country.toString() + lmList.ResponseTime.toString() + lmList.timeset.toString()您不是试图添加字符串,而是尝试添加节点,而这些节点恰好包含字符串。
发布于 2016-07-14 20:27:24
你的代码看起来应该是:
String appendedString = "${lmList.country}${lmList.ResponseTime}${lmList.timeset}"您得到的异常意味着您正在尝试调用plus()/concat()方法,该方法不是由NodeChildren提供的。
发布于 2016-07-14 21:00:53
假设xml看起来像:
def xml = '''
<Root>
<LastGlobal>
<HeatMap>
<Country>USA</Country>
<ResponseTime>20</ResponseTime>
<TimeSet>10</TimeSet>
</HeatMap>
</LastGlobal>
</Root>
'''下文应说明预期的情况:
def slurped = new XmlSlurper().parseText(xml)
assert slurped.LastGlobal.HeatMap.children()*.text().join() == 'USA2010'https://stackoverflow.com/questions/38383462
复制相似问题