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

how to parse the xml payload from ecc queue input response?

chandra_ym
Kilo Expert

as part of REST response, i need to parse the error message i get back in the input ecc queue record's payload. For example: below is the payload which contains the error message:

 

<results error="Unknown host">
<parameters>
<parameter name="http_status_code" value="0"/>
</parameters>
</results>

i would like to extract the value of the "name" attribute (i.e., 0) as part of my custom business rule i am developing?

how do i do this?

4 REPLIES 4

Balaji Jagannat
Kilo Guru

Try something like below

 

var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(payload+'');

var resultdata = xmlDoc.getNodeText("//parameter");

hi Balaji, thanks for responding. the way you suggested doesn't seem to work. Below is the contents of payload field that i get. I need to parse through this and get the value "content" node which has a JSON name value pairs.. attached is the payload.find_real_file.png

@chandra_ym  I  was also searching for something like this and I found the solution. What you are looking for is to get the Value of the attribute "name" in the <parameter name="http_status_code" value="0"/>

 

To get the value of the attribute name use

iterate to the child nodes and using the node name 

<node-name>.getAttribute("name")); 

 

Use this link to understand the concept better : https://www.servicenow.com/community/developer-articles/integratenow-chapter-6-working-with-xmls/ta-...

 

 

 

Weird
Mega Sage

I have done something similar before by converting the value to JSON.
Here's an example I made by adding your payload into an ecc queue record and then parcing the payload from there:

var ecc = new GlideRecord('ecc_queue');
ecc.get("013bc8a20771211074a9f16c7c1ed050");
var responseBody = ecc.payload;

var jsonObj = gs.xmlToJSON(responseBody); // Converting XML into a JSON object
var resultJson = jsonObj["results"]["parameters"]["parameter"]["name"];//Simplify object by taking the inner part with info
gs.info(JSON.stringify(resultJson));

I've usually used this in integrations that use XML, so it should work perfectly for you as well since the responseBody contains all the info.
This is very brone to breaking though. You have to make sure to also use try catch to avoid stuff breaking.
For example if for some reason the rest response doesn't contain the XML structure like I've specified in resultJson, then it will be undefined. One such case would be if there were no parameters for some reason.

Also, if you want the value, you can just change "name" to "value". If there are many parameters, then you'd want to use a loop somewhere. Like so for example:

var jsonObj = gs.xmlToJSON(responseBody); // Converting XML into a JSON object
var resultJson = jsonObj["results"]["parameters"]["parameter"];//Simplify object by taking the inner part with info
for(var i in resultJson){
gs.info(JSON.stringify(resultJson[i]["name"]));
}

If the xml was
<results error="Unknown host">
<parameters>
<parameter name="http_status_code" value="0"/>
<parameter name="http_status_code1" value="10"/>
</parameters>
</results>
The log would return http_status_code and http_status_code1