Contributor
Contributor

Parsing XML/SOAP Response

Hi, 

I want to parse Soap Response  to find a specific element and return this element into the array for vRealize Automation.

I want to take the element between <item> with three letters. Example : <item xsi:type="xsd:string">ABM</item>

I know that exist E4X parsing. But for me it doesn't work correctly.

Here my code to find the element based on REGEX :

// First Make a Rest Request

System.log("Host: " + restHost + ", operation: " + operationUrl + ", Request Type: " );

var requestType = "POST";

var operationUrl = "/Bases/Webservices_CENOTE01.nsf/Equipement?WSDL";

var requestContent =  '<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:DefaultNamespace">    <soapenv:Header/>    <soapenv:Body>       <urn:GETLISTETRIGRAMMES soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>    </soapenv:Body> </soapenv:Envelope>';

var request = restHost.createRequest(requestType, operationUrl, requestContent);

//System.log("request: " + request.fullUrl);

var response = request.execute();

//System.log("response: " + response.contentAsString);

result = new Properties();

result.put("statusCode", response.statusCode);

result.put("contentLength", response.contentLength);

result.put("headers", response.getAllHeaders());

result.put("contentAsString", response.contentAsString);

// Take response

var xmlresponse = response.contentAsString;

var regexp = /(\>(...)\<)/gi;

var ParseResponse = xmlresponse.match(regexp);

var stringtext = ParseResponse.toString();

var replace = stringtext.replace(/[><]/g,'');

var array = replace.split(",");

System.log (array);

return array;

My question is :  Do you know another way to find my element with E4X.

Thank you

Best Regard

Mikael Lelouch

Tags (4)
0 Kudos
1 Reply
VMware Employee
VMware Employee

Hi,

Something like the following should work:

var doc = new XML(xmlresponse);

var soapenv = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");

var ns1 = new Namespace("urn:DefaultNamespace");

var data = doc.soapenv::Body.ns1::GETLISTETRIGRAMMESResponse.GETLISTETRIGRAMMESReturn.LISTE.item;

var array = [];

for each (var x in data) {

  array.push(x);

}

System.log(array);

0 Kudos