无法从xml中检索数据以放入数组中

时间:2017-05-25 10:11:00

标签: groovy soapui

我正在尝试从xml中检索一些flightids并将它们放在一个数组中,但我一直没有显示数据。它似乎没有找到“航班”,但我不确定为什么,下面的代码有什么问题吗?

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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在您的情况下,{p> enyo pack it.name()NodeChild

所以,it只返回一个没有前缀的名称。这意味着您应该将其与it.name()(没有FlightId

进行比较

如果要检查命名空间(链接到前缀),则查找必须如下:

b: