2017-05-25 41 views
1

我想從xml中檢索一定數量的塊,並將它們放入數組中,但我一直沒有顯示數據。它似乎沒有找到'航班',但我不確定爲什麼,下面的代碼有什麼問題?無法從xml中檢索數據以放置在數組中

import groovy.xml.XmlUtil 
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def response = context.expand('${SOAP Request#Response}' ) 
def parsedxml = new XmlSlurper().parseText(response) 


def tests= parsedxml.'**'.findAll { it.name() == 'b:TestId'} 

log.info tests 

//Get the rate plan codes 
    def testId = { option -> 
     def res = option.'**'.findAll {it.name() == 'b:TestId'} 
     if (res) return option.TestId.text() 
     null 
    } 


    def testIdArray = [] 
    tests.each { if (testId(it)) testIdArray << testId(it) } 
    for (int i = 0; i < testIdArray.size(); i++) { 
      log.error "TestIds: " + testIdArray[i] 
    } 


log.warn testIdArray.size() 

下面是XML:

<s:Envelope xxx="xxx" xxx="xxx"> 
    <s:Header> 
     <a:Action s:mustUnderstand="1">xxx</a:Action> 
    </s:Header> 
    <s:Body> 
     <XML1 xmlns="xxx"> 
      <XML2 xmlns:b="xxx" xmlns:i="xxx">    
         <XML3> 
         <b:TestId>000000</b:TestId> 
         </XML3> 
         <XML3> 
         <b:TestId>000000</b:TestId> 
         </XML3>     
      </XML2> 
     </XML1> 
</s:Body> 
</s:Envelope> 
+0

1 /我沒有看到任何'FlightSearchResult'標籤關閉,並且xml不正確 – daggett

+0

您是否在尋找入站航班ID?或出境航班ID?或兩者? – Rao

回答

1

將XML字符串響應下面響應變量

def xml = new XmlSlurper().parseText(response) 

def getFlightIds = { type = '' -> 
    type ? xml.'**'.findAll { it.name() == type }.collect { it.FlightId.text() } : xml.'**'.findAll {it.FlightId.text()} 
} 

//Get the respective flight ids 
//Use the desired one as you have mentioned none 
def inFlightIds = getFlightIds('InboundFlightInformation') 
def outFlightIds = getFlightIds('OutboundFlightInformation') 
def allFlightIds = getFlightIds() 


log.info "Inbound flight ids: ${inFlightIds}" 
log.info "Outbound flight ids: ${outFlightIds}" 
log.info "All flight ids: ${allFlightIds}" 

您可以快速地嘗試網上Demo

+0

非常感謝Rao – BruceyBandit

+0

@BruceyBandit,很高興幫助。欣賞它是否可以upvoted。 – Rao

+0

我一直忘記upvote大聲笑 – BruceyBandit

0

it.name()你的情況itNodeChild

所以,it.name()回報只是一個沒有前綴名。這意味着你應該把它比作FlightId(不b:

,如果你想查詢一個命名空間(與前綴),那麼你的查找必須是這樣的:

def flights = parsedxml.'**'.findAll { it.name() == 'FlightId' && it.namespaceURI()=='xxx' }