2017-04-13 75 views
0

我是Groovy腳本的新手,我需要一些幫助來根據不同的標記獲取標記值。 以下是我通過SOAP從我的應用程序收到的響應。使用Groovy腳本讀取基於其他標記的XML標記值

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soap:Body> 
    <DataResponse xmlns="http://eh.dummy.com"> 
    <onlinePartyData xmlns=""> 
     <tabs xmlns="http://eh.dummy.com"> 
      <tabs_InnerSet> 
       <Tabs_InnerSet_TupleType> 
       <tab> 
        <DISPLAY_TEXT>DESCRIPTION 1</DISPLAY_TEXT> 
        <qsets> 
         <qsets_InnerSet> 
          <Qsets_InnerSet_TupleType> 
          <qset> 
           <SET_CD>QS1</SET_CD> 
           <VISIBLE>true</VISIBLE> 
          </qset> 
          </Qsets_InnerSet_TupleType> 
          <Qsets_InnerSet_TupleType> 
          <qset> 
           <SET_CD>QS2</SET_CD> 
           <VISIBLE>true</VISIBLE> 
          </qset> 
          <qset> 
           <SET_CD>QS3</SET_CD> 
           <VISIBLE>false</VISIBLE> 
          </qset> 
          </Qsets_InnerSet_TupleType> 

         </qsets_InnerSet> 
        </qsets> 
       </tab> 
       </Tabs_InnerSet_TupleType> 
      </tabs_InnerSet> 
     </tabs> 
    </onlinePartyData> 
    </DataResponse> 
</soap:Body> 
</soap:Envelope> 

如果VISIBLE節點爲true,我需要將SET_CD標記值連接起來。在這個例子中,我需要將我的groovy腳本輸出爲QS1,QS2。我怎樣才能做到這一點?

謝謝!

回答

0

這是查詢xml的腳本,其中VISIBLE = true和提取SET_CD值並加入它們。

請注意,問題中提供的xml格式不正確。修正了測試過程。

Groovy腳本

//Pass the xml string to parseText method 
def parsedXml = new XmlSlurper().parseText(xml) 
def result = parsedXml.'**'.findAll {it.VISIBLE == true}.SET_CD.join(',') 
println result 
assert result instanceof String 

如果你想快速的在線試試這個Demo