为Karate上的数据库调用返回的XML文件分配多个JSON值



对于一个测试项目,我正在调用数据库,并获取必要的字段,我将在xml文件中设置这些字段以进行SOAP服务调用。

据我所见,数据库调用以JSON值的形式返回,因此我很难将值分配给xml的">some"部分。

假设我有一个.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xxxxxx>
<soapenv:Header/>
<soapenv:Body>
<int:createSubscriber soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<custBean xxxxx>
<accountNumber xsi:type="xsd:string">#(accountNo)</accountNumber>
<custName xsi:type="xsd:string" xs:type="type:string">Xbox</custName>
</custBean>
<addSubscriberBean xxxxx>7
<subscriberID xsi:type="xsd:string">#(subsID)</subscriberID>
<password xsi:type="xsd:string" xs:type="type:string">0</password>
<areaID xsi:type="xsd:string" xs:type="type:string">1</areaID>
<lineOfCredit xsi:type="xsd:int" xs:type="type:int"></lineOfCredit>
<creditCycle xsi:type="xsd:int" xs:type="type:int"></creditCycle>
<points xsi:type="xsd:int" xs:type="type:int"></points>
<bandwidth xsi:type="xsd:int" xs:type="type:int"></bandwidth>
<physicalPorts xsi:type="xsd:string" xs:type="type:string">8080</physicalPorts>
<mobilePhoneNo xsi:type="xsd:string" xs:type="type:string">#(mobile)</mobilePhoneNo>
<stbCount xsi:type="xsd:int" xs:type="type:int">5</stbCount>
<oTTCount xsi:type="xsd:int" xs:type="type:int">10</oTTCount>
<subscriptionType xsi:type="xsd:string" xs:type="type:string">#(subsType)</subscriptionType>
</addSubscriberBean>
<sequenceID xxxxx>1234567840123422700</sequenceID>
</int:createSubscriber>

然后我打电话到数据库,所以我得到了以下回复:

[
{
"ACCOUNT_NO": "123",
"SUBSCRIBER_ID": "123456",
"MOBILE": "123456789",
"SUBSCRIBER_TYPE": "XXXX"
},
{
"ACCOUNT_NO": "456",
"SUBSCRIBER_ID": "456789",
"MOBILE": "456789123",
"SUBSCRIBER_TYPE": "YYYY"
}
]

因此,正如您在上面看到的那样,我返回了2个对象,这意味着我需要动态地将这些值分配给变量。

找不到任何线索来引导我找到一个解决方案,在该解决方案中,我可以转换JSON类型值,将它们分配给基于XML的变量。

在JSON上循环并构建一个XML。

* def xml = <users></users>
* def fun =
"""
function(u, i) {
var base = '/users/user[' + (i + 1) + ']/';
karate.set('xml', base + 'account', u.accountNo);
karate.set('xml', base + 'mobile', u.mobile);
karate.set('xml', base + 'type', u.subsType);
}
"""
* karate.forEach(users, fun)

最新更新