2012-08-03 89 views
1

我有以下SOAP響應:閱讀條件XML節點Groovy

我需要選擇地址和nId有nIdType =「ACTIVE」。

可能會有更多的地址和nId,我需要選擇第一場比賽。

我寫Groovy腳本,並沒有得到任何的成功。請幫我,我新

可能有可能所有的地址可能會或那下跪具有NID

我有試驗性質,我需要同時更新地址和NID

我需要通過Groovy腳本來實現WH

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
     <ns2:GetD xmlns:ns2="http://xyxz/pqr" xmlns:ns3="http://pqp/ptr" xmlns:ns4="http://nhgg./ns"> 
      <ns2:du> 
       <ns2:Address>UUUUUU</ns2:macAddress> 
      </ns2:du> 
      <ns2:du> 
       <ns2:Address>XXXXXXX</ns2:macAddress> 
      </ns2:du> 
      <ns2:du> 
       <ns2:Address>PQWWEEE</ns2:macAddress> 
      <ns2:dP> 
       <ns2:pN>1</ns2:pN> 
       <ns2:sE> 
        <ns2:nId>08767727</ns2:nId> 
        <ns2:nIdType>ACTIVE</ns2:nIdType> 
       <ns2:sE> 
      </ns2:dP> 
      </ns2:du> 
      <ns2:du> 
       <ns2:Address>TTTTTTTT</ns2:macAddress> 
      </ns2:du> 
     </ns2:GetD> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

回答

2

你可以這樣做(我不得不添加結束</SOAP-ENV:Envelope>標記您的XML和改變</ns2:macAddress></ns2:Address>,使其有效的XML)

def xml = '''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
      | <SOAP-ENV:Header/> 
      | <SOAP-ENV:Body> 
      | <ns2:GetD xmlns:ns2="http://xyxz/pqr" xmlns:ns3="http://pqp/ptr" xmlns:ns4="http://nhgg./ns"> 
      |  <ns2:du> 
      |  <ns2:Address>UUUUUU</ns2:Address> 
      |  </ns2:du> 
      |  <ns2:du> 
      |  <ns2:Address>XXXXXXX</ns2:Address> 
      |  </ns2:du> 
      |  <ns2:du> 
      |  <ns2:Address>PQWWEEE</ns2:Address> 
      |  <ns2:dP> 
      |   <ns2:pN>1</ns2:pN> 
      |   <ns2:sE> 
      |   <ns2:nId>08767727</ns2:nId> 
      |   <ns2:nIdType>ACTIVE</ns2:nIdType> 
      |   </ns2:sE> 
      |  </ns2:dP> 
      |  </ns2:du> 
      |  <ns2:du> 
      |  <ns2:Address>TTTTTTTT</ns2:Address> 
      |  </ns2:du> 
      | </ns2:GetD> 
      | </SOAP-ENV:Body> 
      |</SOAP-ENV:Envelope>'''.stripMargin() 

def a = new XmlSlurper().parseText(xml).Body?.GetD?.du?.find { node -> 
    node.dP?.sE?.nIdType.text() == 'ACTIVE' 
} 

println "First Active Address = ${a?.Address?.text()}" 

,打印:

First Active Address = PQWWEEE 

但很難從你的問題告訴你到底是什麼

+0

後非常感謝..我無法硬編碼XML,因爲這個響應來自於我在大自然中動態的服務.. def response = context.expand('$ {getD#Response}')我可以使用響應而不是你的xml變量 – user1574409 2012-08-03 15:12:37

+0

@ user1574409是的,你應該是abl e(假設'response'是一個包含有效XML的字符串)。我只是爲了一個工作示例對它進行了硬編碼 – 2012-08-03 15:23:59

+0

感謝Tim ..它工作並滿足我的要求。如果我想打印所有具有nIdType ==「ACTIVE」的活動地址,則需要在requiremnet上執行什麼操作。我需要做findall而不是所有的,以及如何打印所有循環或其他技術是否存在 – user1574409 2012-08-03 17:59:49