2016-07-14 27 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'