how to parse the xml payload from ecc queue input response?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2020 08:07 PM
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?
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2020 08:14 PM
Try something like below
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(payload+'');
var resultdata = xmlDoc.getNodeText("//parameter");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2020 08:40 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 01:34 AM - edited 03-28-2023 01:37 AM
@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-...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 02:27 AM
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