How to parse/get the XML response in rest webservice

Jitu Sharma
Tera Contributor

Hi All,

We are doing Service Now integration with Remedyforce and SessionId generated from RestMassage with XML response.

find_real_file.png

We are tried to parse the Rest XML response in Business Rule but unable to parse data.

i am using below code

try {

var r = new sn_ws.RESTMessageV2('Remedyforce SessionID', 'Post');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

gs.addInfoMessage(response.getBody());

gs.addInfoMessage(httpStatus);

//We have tried both formats but its not working for us

/*var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(ResponseBody);

var sessionId = xmlDoc.getNodeText("//sessionId");*/

var xmlDoc = new XMLDocument2(responseBody);

gs.addInfoMessage(xmlDoc.getNodeText("//sessionId"));

var sessionId1 = xmlDoc.getNodeText("/loginResponse/result/sessionId");

gs.addInfoMessage(sessionId1);

}

catch(ex) {

var message = ex.getMessage();

}

//Above code gives the below result:

find_real_file.png

response.getBody() give plain text without tag instead of XML response in Business Rule.

How to get XML response in Business Rule?

Thanks,

Jitendra Sharma

1 ACCEPTED SOLUTION

jobby
Giga Contributor

Hi Jitu,



Please have a look at below code...



var s = '<?xml version="1.0" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:inc=\"http://www.service-now.com/incident\"><soapenv:Body><loginResponse><result><sessionId>12364</sessionId></result></loginResponse></soapenv:Body></soapenv:Envelope>';



var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(s);


var node = xmlDoc.getNodeText('//sessionId');



gs.print(node)




Replace 's' 'with your responseBody



Please let me know if it works..



Regards,


Jobby EK


View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Jitu,



Any update on this?


Can you mark answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader