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-07-2020 12:16 AM
Now I am getting the attributes and values for one object but not all.
I tried to convert to JSON. Its not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2020 12:36 AM
Hi,
are you not able to convert xml to json?
please share the actual xml you are having
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-07-2020 12:41 AM
xmlToJSON(xml string) doesn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2020 12:43 AM
It gives null.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2020 01:00 AM
Hi,
it means your xml is not valid
the link I shared I just tried same script
var xTest = "<a>Hello World</a>";
gs.info(xTest);
var obj = gs.xmlToJSON(xTest);
gs.info(JSON.stringify(obj));
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader