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 11:06 AM
Why not try converting it to JSON? Based on KB0957116 that might make it easier to parse. Also, unless I'm mistaken since all nodes are named "ReferenceType," you'll need to loop through them. Try this:
var pendingdate = '';
var SoapRequestBody = source.u_message_xml.toString();
gs.log('soap body is' + SoapRequestBody);
if (JSUtil.notNil(SoapRequestBody)) {
var jsonObj = gs.xmlToJSON(SoapRequestBody); // Convert to JSON Object
try {
gs.log('inside try');
// Loop through the jsonObj looking for "Pending Until" and pull the
// ReferenceValue when found.
//
for (var i = 0; i < jsonObj.length; i++) {
if (jsonObj[i].ReferenceType == 'Pending Until')
pendingdate = jsonObj[i].ReferenceValue;
}
} catch (e) {
}
}
Apologies if the above doesn't work. I don't have access to my PDI so I can't verify the code.
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 11:29 AM
Thanks for your response, code is not working..
It is giving the undefined length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 11:55 AM - edited 05-22-2025 11:55 AM
Sorry about that, I wasn't able to test before. Try updating the for loop as follows:
for (var i = 0; i < jsonObj.DocumentReference.length; i++) {
if (jsonObj.DocumentReference[i].ReferenceType == 'Pending Until')
pendingdate = jsonObj.DocumentReference[i].ReferenceValue;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2025 09:54 PM
Still the length is undefined...