vijaydi
10 years agoContributor
removeDomNodes with for Loop
Hi,
Below is the portion of the XML request that I am manuplating using groovy script.
Based on a value ranging from 1-5 (which I read from a file), I will have to remove the Domnodes. For eg. If the value is 3, then I will have to remove nodes 4 and 5.
I have the below groovy script but what I observe is that it does not remove all the nodes as the position of the nodes change when the loop is exeuted for the first loop value.
On exeuting this (with the noAccounts = 2), I get the below updated XML request
I would apprecite if anyone could help me with the solution.
Thank you.
Below is the portion of the XML request that I am manuplating using groovy script.
<Accounts>
<account recordId="1">
<account recordId="2">
<account recordId="3">
<account recordId="4">
<account recordId="5">
<Accounts>
Based on a value ranging from 1-5 (which I read from a file), I will have to remove the Domnodes. For eg. If the value is 3, then I will have to remove nodes 4 and 5.
I have the below groovy script but what I observe is that it does not remove all the nodes as the position of the nodes change when the loop is exeuted for the first loop value.
def ReqHolder2 = grUtils.getXmlHolder("Modified#Request")
def count = ReqHolder2.getDomNodes('//ARCAAccountReportBatch[1]/Accounts[1]/Account').size()
log.info(count)
noAccounts = Integer.parseInt (context.expand( '${DataSource#noAccounts}' )) ///numeric value I read from the file
for (i in noAccounts+1..count)
{
ReqHolder2.removeDomNodes("//ARCAAccountReportBatch[1]/Accounts[1]/Account[$i]")
}
ReqHolder2.updateProperty()
On exeuting this (with the noAccounts = 2), I get the below updated XML request
<Accounts>
<account recordId="1">
<account recordId="2">
<account recordId="3">
<account recordId="5">
<Accounts>
I would apprecite if anyone could help me with the solution.
Thank you.