Get a value from XML Request body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 10:36 AM
Hello Developers :
My requirement is to fetch the specific value from the XML Request body.
Below is the code written.
It is not giving the result of line 331, I think something with the line - 330.
It is executing well till 328
Below is the attached xml request - from this I want to get the value of Pending Until i.e.
<ReferenceValue>2025-04-29T10:00:00</ReferenceValue> I want to fetch this date & time.
Please help to correct my code or suggest something new line of code to get the desired value from XML.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 10:07 PM
I have stringfy the obj, now the payload in coming in json but still the length is undefined..
we have 2 arrays of document reference in json body... which contains different values in it..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2025 01:49 PM
Ah okay. I can't see your entire XML payload, but essentially you need to update your dot-walks to represent each node in the XML structure. Nodes with the same name will become an array containing their respective properties and objects. Your code is returning "undefined" because that object, property or array doesn't exist.
When you convert the XML to JSON, each node becomes a nested object or property within the new JavaScript object. For example, take into consideration the following XML:
<Root>
<Parent>
<Child>
<Value>123</Value>
</Child>
</Parent>
<Parent>
<Child>
<Value>456</Value>
</Child>
</Parent>
</Root>
When this is converted, it will become objects and arrays representing those nodes and values. You can run this in a background script to experiment:
var xml = '<Root><Parent><Child><Value>Value in First Nest</Value></Child></Parent><Parent><Child><Value>Value in Second Nest</Value></Child></Parent></Root>';
var jsonObj = gs.xmlToJSON(xml);
// Value in the First Parent
gs.print(jsonObj.Root.Parent[0].Child.Value);
// Value in the Second Parent
gs.print(jsonObj.Root.Parent[1].Child.Value);
// This will return "undefined"
gs.print(jsonObj.Parent[1].Child.Value);
Note that "Root" has to be added to access each child object. "Parent" becomes an array because the XML contains nodes with the same name. "Child" is an object that contains the property "Value."
Taking all of this into consideration, your for loop will need to be updated as follows:
for (var i = 0; i < jsonObj.<parent_node(s)>.DocumentReference.length; i++) {
if (jsonObj.<parent_node(s)>.DocumentReference[i].ReferenceType == 'Pending Until')
pendingdate = jsonObj.<parent_node(s)>.DocumentReference[i].ReferenceValue;
}
Replace "<parent_node(s)>" with any parent node in your XML structure. This loop also assumes that "ReferenceType" exists for each iteration of "DocumentReference," and "DocumentReference" is an array of objects containing the properties you need. If this isn't always true, then "ReferenceType" will also return "undefined" and you will need to test for that. You can also assign parts of your object to a new variable if the dot notation is becoming cumbersome.
It is also possible that the parent node is also an array, which if true means you'd need to iterate through each of those with nested loops (This can get messy very fast).
In these types of situations it is common to have unexpected results or not always return the value you are expecting. I encourage you to try playing around with this data structure in a background script to understand how to access what you need.
If you still encounter issues, please provide an example of your entire XML structure (sanitized) that I can test with.
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 11:28 AM
Hi @Dinesh90 ,
Are you reading this soap based xml in some transform map.
Did you check the line 329 with getNode() instead of getNodeText() or add the 'DocumentReference in path like below.
if(xmlDocNew.getNodeText('//DocumentReference/RefereneceType') == 'Pending Until')
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 11:39 AM - edited 05-22-2025 11:41 AM
Hello Ashish,
Yes I am reading it in transform map. Tried with your suggestion as well -
It is not executing line 330.. not giving log on 331 , till 328 it is fine.
Please let me know where is the mistake