如何将soap xml响应转换为分隔符

时间:2016-06-25 20:03:05

标签: groovy soapui cdata

我对XML几乎一无所知。我已成功从asmx Web服务获得SOAP响应(使用SOAPUi和Boomerang之类的东西)。它是一个大文件。

现在我需要将它定位到常规分隔列。有一个简单的方法吗?

我的文件附有here

1 个答案:

答案 0 :(得分:1)

不确定是否需要进行一次转换或经常完成此工作。

所以,在这里添加一些更详细的答案。

方法#1:使用在线

如评论中所述,您可以使用on-line site转换xml data into csv

即使它需要使用您拥有的消息/响应进行一些预处理,即

  • 将数据保存到文件
  • 删除标题或不需要的数据等,或准备好在上述在线网站中使用。

此方法中的缺点

  • 需要一些手动工作
  • 公开数据,但有时可能会分享
  • 拍摄时间
  • 不能以自动方式使用
  • 难以重复

方法#2:使用 Groovy脚本

因此,这种方法解决了#1方法的缺点。

这是Groovy脚本,它读取之前的soap请求步骤的响应,并将数据提供给csv文件。

  • 在您的测试用例中,在肥皂请求步骤后立即添加新的groovy script 测试步骤 ,该步骤为您提供数据并将脚本内容复制到其中。即,(测试用例 - >
    第1步:肥皂请求,您将得到答复
    第2步:Groovy脚本(以下脚本))
  • 添加测试用例自定义属性,例如OUTPUT_FILE_NAME,并提供要保存的csv的文件路径。即使如果您不提供此属性,它也会自动将csv文件chargedata.csv保存在系统临时目录下。

您可以在线找到评论

/**
* this script will read the previous step response
* extract the cdata at the given xpath
* read all the records and transfroms into csv file
**/

import com.eviware.soapui.support.XmlHolder
import groovy.xml.*

/**Define the output file name in test case custom property say OUTPUT_FILE_NAME and value as absolute file path
* otherwise, it write a file chargedata.csv in system temp directory
**/
def outputFileName = context.testCase.getPropertyValue('OUTPUT_FILE_NAME') ?: System.getProperty("java.io.tmpdir")+ '/chargedata.csv'

//csv field separator - change it if needed
def delimiter = ','

/**
* Below statement will fetch the previous request step response. 
*/
def response = context.testCase.testStepList[context.currentStepIndex - 1].testRequest.response.responseContent

//Create the xml holder object to get the xpath value which cdata in this case
def responseHolder = new XmlHolder(response)
def xpath = '//*:Charges_FileResponse/*:Charges_FileResult'

//Get the cdata part from above xpath which is a string
def data = responseHolder.getNodeValue(xpath)

//This again parses the xml inside of cdata
def chargeRecords = new XmlParser().parseText(data)

//This is going hold all the data from ChargeRecords
def chargeRecordsDataStructure = []

//This is to hold all the headers
def headers = [] as Set

/**
* This is to create Charge data
**/
def buildChargeDataStructure = { charge ->
    def chargeDataStructure = new Expando()
    charge.children().each {
        def elementName = it.name()
        def elementText = it.value().join()
        chargeDataStructure[elementName] = elementText
        //Add to field name to the list if not already added
        (elementName in headers) ?: headers << elementName
    }
    chargeDataStructure
}

/**
* this is to create a csv row in string format
**/
def createRow = { recordDataStructure ->
    def row = new StringBuffer()
    headers.each {
        if (row) {
            row += delimiter + recordDataStructure[it] ?: ''
        } else {
            row += recordDataStructure[it] ?: ''
        }
    }
    row.toString()+'\n'
}

//Build the whole data structure of Charge Records
chargeRecords.Charge.each { charge ->
    chargeRecordsDataStructure << buildChargeDataStructure( charge )
}

//Build the rows
def rows = new StringBuffer()
rows << headers.join(',') +'\n'
chargeRecordsDataStructure.each { rows << createRow (it)}

//Write the rows into file
new File(outputFileName).text = rows