如何使用空手道从XML响应中的字符串中提取值

问题描述 投票:1回答:1

不幸的是,从我的后端获得的响应不是正确的xml格式,而是以这样的错误格式给出响应:

<soapenv:Body>

    <ns2:getInputResponse xmlns:ns2="http://docs.oasisopen.org/ns/bpel4people/ws-humantask/api/200803">

        <ns2:taskData xmlns:s186="http://www.w3.org/2001/XMLSchema-instance" xmlns:s187="http://www.w3.org/2001/XMLSchema" s186:type="s187:string">&lt;?xml version="1.0" encoding="UTF-8"?>

&lt;SubscriptionApprovalData xmlns="http://workflow.subscription.apimgt.carbon.wso2.org" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

     &lt;apiName>Auto_Approval&lt;/apiName>

     &lt;apiVersion>v1&lt;/apiVersion>

     &lt;apiContext>/test/lambda/v1&lt;/apiContext>

     &lt;apiProvider>admin&lt;/apiProvider>

     &lt;subscriber>regtest&lt;/subscriber>

     &lt;applicationName>newApp&lt;/applicationName>

     &lt;tierName>Gold&lt;/tierName>

     &lt;workflowExternalRef&gt;23d30bd8-51e3-4afe-aae0-3fa159d85a6b&lt;/workflowExternalRef>

     &lt;callBackURL>https://apistore-dev-dev-a878-14-ams10-nonp.qcpaws.qantas.com.au/services/WorkflowCallbackService&lt;/callBackURL>
  &lt;/SubscriptionApprovalData></ns2:taskData>

    </ns2:getInputResponse>

</soapenv:Body>

现在因为这个空手道无法读取响应并获取“workflowExternalRef”的值,这是我测试的目标。

空手道有什么方法可以阅读吗?

dsl karate
1个回答
0
投票

这真的搞砸了XML,所以如果可以修复,请与团队成员核实。

无论如何,既然你可以在空手道中使用Java,这是一种方法。这不是生产质量的代码,请适当调整:

* string response = response
* def start = response.indexOf('workflowExternalRef&gt;')
* def ref = response.substring(start + 23)
* def end = ref.indexOf('&lt;')
* def ref = ref.substring(0, end)
* match ref == '23d30bd8-51e3-4afe-aae0-3fa159d85a6b'
© www.soinside.com 2019 - 2024. All rights reserved.