我对XML几乎一无所知。我已成功从asmx
Web服务获得SOAP响应(使用SOAPUi和Boomerang之类的东西)。它是一个大文件。
现在我需要将它定位到常规分隔列。有一个简单的方法吗?
我的文件附有here
答案 0 :(得分:1)
不确定是否需要进行一次转换或经常完成此工作。
所以,在这里添加一些更详细的答案。
方法#1:使用在线
如评论中所述,您可以使用on-line site转换xml data into csv
。
即使它需要使用您拥有的消息/响应进行一些预处理,即
此方法中的缺点
方法#2:使用 Groovy脚本
因此,这种方法解决了#1方法的缺点。
这是Groovy脚本,它读取之前的soap请求步骤的响应,并将数据提供给csv
文件。
groovy script
测试步骤 ,该步骤为您提供数据并将脚本内容复制到其中。即,(测试用例 - > 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