Soap UI 格式 XML 响应到 csv groovy

Soap UI format XML reponse into csv with groovy

我是 SOAP UI 和 Groovy 的新手,我已经尝试了几天来解决它,但我无法实现。有人知道如何正确格式化吗?

我需要将“Preis”节点正确附加到此格式并删除“VOKey”节点。

我需要以下输出格式的响应:

目前我是这样理解的:

我的XML回复:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns1:calculateOfferteResponse xmlns:ns1="http://www.testtest.wsdl/OfferteWebservice">
         <ns162:returnValue xmlns:ns162="http://www.testtest.wsdl/OfferteWebservice">   
         <Mutationsgrund>-1</Mutationsgrund>
            <ParamAttribute>
               <VOKey id="1234" type="-500388"/>
               <ParamAttrDef>BP</ParamAttrDef>
            </ParamAttribute>
            <ParamAttribute>
               <VOKey id="1243"-500388"/>
               <ParamAttrDef>DATUM</ParamAttrDef>
            </ParamAttribute>
            <ParamAttribute>
               <VOKey id="1122" type="-500388"/>
               <ParamAttrDef>STATUS</ParamAttrDef>
            </ParamAttribute>
            <ParamAttribute>
               <VOKey id="2222" type="-500388"/>
               <ParamAttrDef>AZ1</ParamAttrDef>
            </ParamAttribute>
            <ParamAttribute>
               <VOKey id="3333" type="-500388"/>
               <ParamAttrDef>ADR_CO</ParamAttrDef>
            </ParamAttribute>
            <MessageContainer/>
            <Offerte>
               <ParamAttribute>
                  <VOKey id="4444" type="-500393"/>
                  <ParamAttrDef>MAKLERNR</ParamAttrDef>
                  <Wert>MAK37856378</Wert>
               </ParamAttribute>
               <Produktsparte>KUNDE</Produktsparte>
               <RahmenvertragNr>K110106</RahmenvertragNr>
               <Preis>204.83</Preis>
               <VersicherteObjekte>
                  <VOKey id="5555" type="-500183"/>
                  <ParamAttribute>
                     <VOKey id="6666" type="-500392"/>
                     <ParamAttrDef>VERSICHERUNG</ParamAttrDef>
                     <Wert>112233445</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="1232" type="-500392"/>
                     <ParamAttrDef>KUENDIGUNG</ParamAttrDef>
                     <Wert>Nein</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="4456" type="-500392"/>
                     <ParamAttrDef>BEITRAGSFREI</ParamAttrDef>
                     <Wert>Nein</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="1654" type="-500392"/>
                     <ParamAttrDef>BERUFSBEZEICHNUNG_FREITEXT</ParamAttrDef>
                     <Wert>Wirt</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="4423 type="-500392"/>
                     <ParamAttrDef>BEZ_ZUM_VN</ParamAttrDef>
                     <Wert>VN</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="9898" type="-500392"/>
                     <ParamAttrDef>BERECHTIGUNG</ParamAttrDef>
                     <Wert>FOLGE</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="7203" type="-500392"/>
                     <ParamAttrDef>DYNAMIK</ParamAttrDef>
                     <Wert>Nein</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="4200" type="-500392"/>
                     <ParamAttrDef>VALIDE</ParamAttrDef>
                     <Wert>150000</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="7365" type="-500392"/>
                     <ParamAttrDef>VERMITTLER</ParamAttrDef>
                     <Wert>0</Wert>
                  </ParamAttribute>
                  <ParamAttribute>
                     <VOKey id="9324" type="-500392"/>
                     <ParamAttrDef>VORNAME</ParamAttrDef>
                     <Wert>Patty</Wert>
                  </ParamAttribute>
               </VersicherteObjekte>
            </Offerte>
         </ns162:returnValue>
      </ns1:calculateOfferteResponse>
   </soapenv:Body>
</soapenv:Envelope>

那是我的 groovy 脚本断言:

 //Change file name as needed
def fileName = 'N:/test.csv'
def delimiter = ';' 

assert context.response, 'Response is empty or null'

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


def personalInfos = xml.'**'.findAll { it.name() == 'ParamAttribute' }
def preis = xmlp.'**'.findAll { it.name() == 'Preis' }

//get all childen from ParamAttribute
def list = personalInfos.collect {info -> info.children()*.name().collectEntries{[(it): info."$it"] } }


def sb = new StringBuffer(list[0].keySet().join(delimiter))

sb.append('\n')


list.collect { sb.append(it.values().join(delimiter)).append('\n')}
log.info "Data going to be written into file: \n ${sb.toString()}"


new File(fileName).append(sb.toString())
new File(fileName).append(preis.toString())



new File(fileName).with{
    append(sb.toString())
    append(preis.toString())
    }

提前致谢!

assert context.response, 'Response is empty or null'

def xml = new XmlSlurper().parseText(context.response)
def personalInfos = xml.'**'.findAll { it.name() == 'ParamAttribute' }
def listOfMaps = personalInfos.collect {info -> 
    info.children().collectEntries{[it.name(), it.text()] }
}
def headers = listOfMaps.collectMany{it.keySet()}.unique().findAll{it!='VOKey'}
def csv = listOfMaps.collect{li-> headers.collect{h->li[h]?:''}.join(';') }.join('\n')

new File(fileName).append(csv)