You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Groovy修改SoapUI请求:动态生成XML键值对及经销商统计

Hey there! Let's tackle this problem step by step—you need to dynamically generate SOAP request XML tags based on the key-value pairs in your Input.txt file to count dealers in the current country, right? Here's a practical, SoapUI-focused solution:

Step 1: Parse the Input.txt File

First, we'll read each line from your Input.txt and extract the key-value pairs. Each line represents a unique set of parameters for your SOAP request. For example, a line like key1=Fruit,value1=Carrot,Key2=Vegetable,value2=Carrot gets split into individual pairs, then each pair is split by = to separate keys and values.

Step 2: Use Groovy Script (SoapUI's Go-To Tool)

SoapUI natively supports Groovy scripting, which is perfect for dynamic XML generation. Here's a ready-to-use script you can attach to your request:

// Point to your Input.txt file (update the path!)
def inputFile = new File("C:/Your/File/Path/Input.txt")
def soapRequest = context.request

// Loop through each line in the file
inputFile.eachLine { line ->
    def paramBuilder = new StringBuilder("<FilterParameters>")
    
    // Split line into key-value pairs
    def pairs = line.split(",")
    pairs.each { pair ->
        def keyValue = pair.split("=")
        def cleanKey = keyValue[0].trim()
        def cleanValue = keyValue[1].trim()
        
        // Generate XML tags (adjust tag names to match your SOAP service's schema!)
        paramBuilder.append("<${cleanKey}>${cleanValue}</${cleanKey}>")
    }
    
    paramBuilder.append("</FilterParameters>")
    
    // Replace a placeholder in your base SOAP request with the dynamic XML
    soapRequest = soapRequest.replace("<!--DYNAMIC_PARAMS-->", paramBuilder.toString())
}

// Update the request context with the modified SOAP body
context.request = soapRequest
Step 3: Set Up Your SoapUI Request
  1. In your base SOAP request, add a placeholder comment where you want the dynamic parameters to inject:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:deal="http://your-dealer-service.com">
   <soapenv:Header/>
   <soapenv:Body>
      <deal:GetDealerCount>
         <deal:Country>CurrentCountry</deal:Country>
         <!--DYNAMIC_PARAMS-->
      </deal:GetDealerCount>
   </soapenv:Body>
</soapenv:Envelope>
  1. Attach the Groovy script to your request: Right-click the request > Add Step > Groovy Script, or paste it into the "Script" tab of the request editor.
  2. Adjust the XML tag names in the script (like <FilterParameters> or <${cleanKey}>) to match exactly what your SOAP service expects.
Step 4: Handle Structured Key-Value Pairs (If Needed)

If your service expects pairs wrapped in a specific structure (e.g., <Parameter><Key>...</Key><Value>...</Value></Parameter>), tweak the script's pair-processing logic:

pairs.eachWithIndex { pair, idx ->
    if (idx % 2 == 0) { // Grab key from even indices, value from next odd index
        def key = pair.split("=")[1].trim()
        def value = pairs[idx+1].split("=")[1].trim()
        paramBuilder.append("<Parameter><Key>${key}</Key><Value>${value}</Value></Parameter>")
    }
}

This works if your Input.txt lines follow the consistent keyN=X,valueN=Y pattern from your examples.

Step 5: Batch Requests (Optional)

If each line in Input.txt needs a separate SOAP call, use SoapUI's built-in tools:

  • Add a File Data Source pointing to Input.txt, configured to split lines by newline.
  • Link the data source to your request using property expansion (e.g., ${DataSource#key1}).
  • Add a Loop test step to iterate through all lines automatically.

内容的提问来源于stack exchange,提问作者lokeshchitturi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:44:16