2016-07-14 120 views
1

我正在解析Groovy中的XML文件,后来我需要附加返回的变量。如何在Groovy中追加多个字符串?

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 
+0

你能给一些示例XML? –

回答

1

作为替代injecteer -

def lmList = slurperResponse.LastGlobal.HeatMap 
String appendedString = lmList.country.toString() + lmList.ResponseTime.toString() + lmList.timeset.toString() 

您并未尝试添加字符串,而是尝试添加包含字符串的节点。

2

尤尔代码应当是这样的:

String appendedString = "${lmList.country}${lmList.ResponseTime}${lmList.timeset}" 

你越来越手段外,您试图调用未提供plus()/concat()方法通过NodeChildren

+0

我喜欢声明你的代码看起来像: - 非常好:) –

1

假设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'