My XML looks like is
<?xml version="1.0" encoding="UTF-8"?>
<TimeAccount>
<TimeAccount>
<accountType>CMSV</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>36218</userId>
<timeAccountDetails/>
</TimeAccount>
<TimeAccount>
<accountType>CMSV</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>abc222</userId>
<timeAccountDetails/>
</TimeAccount>
<TimeAccount>
<accountType>PTOS</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>abc111</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingAmount>176</bookingAmount>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
<TimeAccount>
<accountType>PTOS</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>abc222</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingAmount>256</bookingAmount>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
<TimeAccount>
<bookingStartDate>2021-01-01T00:00:00.000</bookingStartDate>
<accountType>LOA</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>abc111</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingAmount>40</bookingAmount>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingAmount>-8</bookingAmount>
<bookingType>EMPLOYEE_TIME</bookingType>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
<TimeAccount>
<bookingStartDate>2021-01-01T00:00:00.000</bookingStartDate>
<accountType>LOA</accountType>
<bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate>
<userId>abc222</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingAmount>40</bookingAmount>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
</TimeAccount>
I need to add a new node for each employee record when account Type is PTOS
and subnode is MANUAL_ADJUSTMENT
, I wrote below code which works for single employee record, how should I loop for all employee records.
def ptoNode = xml.TimeAccount.find { it.accountType.text() == 'PTOS' }
if(ptoNode !=null)
{
def detailNode = ptoNode.timeAccountDetails.TimeAccountDetail.find { it.bookingType.text() == 'MANUAL_ADJUSTMENT' }
if(detailNode != null)
{
ptosHours = detailNode.bookingAmount.text()
ptoNode.appendNode('newPTONode', null, ptosHours.toString() )
}
}
above code works only for 1st employee (
abc111
), how to do it for each employee node?I need to add all hours (
bookingAmount
) sub nodes with booking typeEMPLOYEE_TIME
with in employee node and create node like above at employee header level, I am able to do it for oneEMPLOYEE_TIME
record (same like above) but could not recursively add all subnodes. how to achieve it?How to loop through XML document at high level and detail level in groovy script?