getChildNodeIterator() iteration more than no of entries

priyankapalaska
Giga Contributor

Hi,

I have request body, where multiple nodes can be there in request body xml. But when I am iterating it using getChildNodeIterator(), it is iterating more times than number of entries parent node has. 

In the below scenario, logs occurring 6-7 times, when Request body has only one <x:systemResponse> node. 

I even tried replacing the statement var sysResp = xmlDoc.getNode('//x:targetResponse') by var sysResp = xmlDoc.getNode('//x:systemResponse'), but same situation was there. 

Request Body XML:

<x:execute>
<x:targetResponse>
<!-- 1 or more repetitions: -->
<x:systemResponse>
<x:system>SAP</x:system>
<x:type>S</x:type>
<x:message>Error Removed</x:message>
</x:systemResponse>
</x:targetResponse>
</x:execute>
 
Processing Script:
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(reqXML);
var sysResp = xmlDoc.getNode('//x:targetResponse');
var iter = sysResp.getChildNodeIterator();

while(iter.next()) {
var system=xmlDoc.getNodeText('//x:system');
var type=xmlDoc.getNodeText('//x:type');
var msg=xmlDoc.getNodeText('//x:message');
gs.info("System = "+system+"\nType = "+type+"\nMsg = "+msg);
}
 
 
Appreciate your quick response. 
 
Thanks you.
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Priyanka,

I believe that is because of the namespace prefix i.e. x

May be you can create a script include in global scope and pass the xml string, parse it and return whatever you want. in that way you would be able to achieve

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Actually we may have multiple <x:systemResponse> nodes, therefore we are applying child node iterator. 

and in the loop, if we have 2 child nodes, it can only able to search one and iterate it multiple (6-7) times.  

Hi Priyanka,

The issue is because of namespace, so better way is to handle this using the global scope.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Priyanka,

Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader