How to read attributes and values from XML payload?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 10:03 PM
Hi ,
I have a requirement to read from xml payload and write it in a staging table along with few other columns read from another table. e.g 3 columns from xml payload and 2 columns from another table which are in string format.
How do I read the attributes and values dynamically ?
the payload looks like : <?xml version="1.0" encoding="UTF-8" standalone = "yes"?>
<set>
<set1>
<attr1> 123 </attr1>
</set1>
<set2>
<attr2>xyz</attr2>
</set2>
<attr3> value1 </attr3>
<attr4>value2</attr4>
</set>
It looks like in tree structure. How to I get the attribute names(attr1,attr2,attr3) and values (123,xyz,value1,value2) ?
The script is one but the payloads are different for different use cases.
Thanks & Regards,
Chinmayee Mishra
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 10:40 PM
Hi,
payload might not be the same.
but the approach of parsing the XML for repeated tags will be same
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 10:42 PM
Hi,
If I paste the above xml and convert it to JSON online , It looks like below
{"set1": {
"attr1": "123" }, "set2": {
"attr2": "xyz" }, "attr3": "value1", "attr4": "value2" }
This is what I need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 10:48 PM
Hi,
you can convert that XML to JSON and then parse the JSON
For converting xml to json refer this link
Script I tried
var jsonString = '{"set1":{"attr1":"123"},"set2":{"attr2":"xyz"},"attr3":"value1","attr4":"value2"}';
var parser = JSON.parse(jsonString);
var attr1Value = parser.set1.attr1;
var attr2Value = parser.set2.attr2;
var attr3Value = parser.attr3;
var attr4Value = parser.attr4;
gs.info(attr1Value);
gs.info(attr2Value);
gs.info(attr3Value);
gs.info(attr4Value);
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 11:28 PM
Thanks for marking my response as helpful.
Let me know if I have answered your question.
If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2020 11:49 PM
Hi Ankur ,
I am able to get the values but not attributes.
I did the below
var payLoad = "my xml payLoad";
var helper = XMLHelper(payLoad);
obj = helper.toObject();
for(x in obj){
gs.log("xml objects :"+obj[x]);
}
It gives me only values of every node but not the attribute names.
Can you please advise ?
Thanks & Regards,
Chinmayee Mishra