The CreatorCon Call for Content is officially open! Get started here.

How to parse the SOAP XML response Message.

shamshadbegum
Tera Contributor

Hi All,

I am tried to parse the SOAP XML response but i am not able to do so. i am using below code

var response = s.execute();

var ResponseBody = response.getBody();

var status = response.getStatusCode();

gs.log(ResponseBody);

var xmldoc = new XMLDocument1(ResponseBody);

var correlationID = xmldoc.getNodeText("//number");

gs.log(correlationID);

it's returning "null"

if i run the below code in script backgroud i am getting error(Javascript compiler exception: missing ; before statement (null.null; line 1) in:). if i remove the <?xml version='1.0' encoding='UTF-8'?> from the responseBody it's working fine.

var ResponseBody ='<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><insertResponse><sys_id>b97fd52c4f69030078253ce18110c731</sys_id><number>INC0010060</number></insertResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';

var xmldoc = new XMLDocument1(ResponseBody);

var correlationID = xmldoc.getNodeText("//number");

gs.log(correlationID);

can some one help me to fix the issue.

thanks and regards,

shamshad

1 ACCEPTED SOLUTION

HI,



Please try the below code, it will work.


there was a mistake with xmlDoc object as it was different from what was declared.



var response = s.execute();


var ResponseBody = response.getBody();


var status = response.getStatusCode();


var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(ResponseBody);


var correlationID = xmlDoc.getNodeText("//number");


gs.log(correlationID);




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


View solution in original post

5 REPLIES 5

Gaurav Bajaj
Mega Sage

Hi,



Please try Scoped XMLDocument2 API Reference - ServiceNow Wiki API as XMLDocument1 is deprecated now.






Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


Ujjawal Vishnoi
Mega Sage

Hi Shamshad,



Please change your script as mentioned below.



var response = s.execute();


var ResponseBody = response.getBody();


var status = response.getStatusCode();


var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(ResponseBody);


var correlationID = xmldoc.getNodeText("//number");


gs.log(correlationID);



Hope this helps.



Regards


Ujjawal


shamshadbegum
Tera Contributor

Hi Ujjawal,



thank you.   I have tried this but unfortunately it's not working.


HI,



Please try the below code, it will work.


there was a mistake with xmlDoc object as it was different from what was declared.



var response = s.execute();


var ResponseBody = response.getBody();


var status = response.getStatusCode();


var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(ResponseBody);


var correlationID = xmlDoc.getNodeText("//number");


gs.log(correlationID);




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav