Parse XML response to another requests in Postman

I was able to come up with a slightly easier solution for reading from the xml response.

var xmlTree = xml2Json(responseBody);
var text = xmlTree["SOAP-ENV:Envelope"]["SOAP-ENV:Body"]["ns1:loginResponse"]["loginReturn"]["_"];
var textstring = JSON.stringify(text);
console.log(textstring);
postman.setEnvironmentVariable("loginReturn", textstring);

Here is the response

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:loginResponse>
            <loginReturn xsi:type="xsd:string">b1c705515532a9abc353b32e91de3b97</loginReturn>
        </ns1:loginResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

HOWEVER, I am not able to utilize the variable I saved to Environment in subsequent request using the following format

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:Magento">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:shoppingCartCreate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <sessionId xsi:type="xsd:string">{{loginReturn}}</sessionId>
         <storeId xsi:type="xsd:string">1</storeId>
      </urn:shoppingCartCreate>
   </soapenv:Body>
</soapenv:Envelope>

It works the regular expression, i use this:

var regularExpression = /^.{0,12}/;
var text = jsonData["soap:Envelope"]["soap:Body"]["ns2:execResponse"]["ns2:execReturn"];

match = regularExpression.exec(text);

Thank you! :)


The trick is how your XML is converted to JSON and how to access JSON data.

Your parsed JSON has the next structure: enter image description here So in order to get a value from it you should get elements step by step like this:

jsonData["xs:schema"]["xs:element"][0]["$"]["name"]

So to set up the variable you need to do this:

postman.setEnvironmentVariable("NumberReq", jsonData["soap:Body"]["ns2:execResponse"]["ns2:execReturn"]);

Hope that example above is exactly the code you need. If not, revise the structure of the parced JSON and change the way to the element you need.

Probably you also can do it using "dot" structure, like jsonData.Body.execResponse.execReturn