groovy - 遍历 Groovy 中的每个 XML 员工记录
问题描述
我的 XML 看起来像
<?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>
PTOS
当帐户类型为且子节点为时,我需要为每个员工记录添加一个新节点MANUAL_ADJUSTMENT
,我在下面编写了适用于单个员工记录的代码,我应该如何循环所有员工记录。
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() )
}
}
以上代码仅适用于第一个员工(
abc111
),如何为每个员工节点执行此操作?我需要在员工节点中添加
bookingAmount
具有预订类型EMPLOYEE_TIME
的所有小时 () 子节点,并在员工标题级别创建类似上面的节点,我可以为一条EMPLOYEE_TIME
记录执行此操作(与上面相同),但无法递归添加所有子节点。如何实现呢?如何在 groovy 脚本中循环遍历高级和详细级别的 XML 文档?
解决方案
def xml=new XmlParser().parseText('''<?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>
''')
xml.TimeAccount.findAll{it.accountType.text()=='PTOS'}.each{ptoNode->
ptoNode.timeAccountDetails.TimeAccountDetail.find{ it.bookingType.text() == 'MANUAL_ADJUSTMENT' }?.with{detailNode->
ptosHours = detailNode.bookingAmount.text()
ptoNode.appendNode('newPTONode', null, ptosHours )
}
}
println groovy.xml.XmlUtil.serialize(xml)
推荐阅读
- svn - 主干(或给定分支)中最新修订的修订说明符
- php - 在每一行中合并具有相同product_id但不同style_id的行
- java - 服务的 Spring Boot 多模块单元测试
- database - 如果在数据库事务发生的那一刻断电会发生什么?
- ms-access - If values in one column is entered populate other columns (Access)
- configuration - Automapper - 添加配置文件后映射失败
- google-cloud-platform - 环境变量 GOOGLE_APPLICATION_CREDENTIALS 在 Google 云上仅持续一天
- matlab - How do I call my generate random numbers which has size 100 to simulation of playing dice 100 times
- javascript - Set body color but div block has different color
- c# - Copy a texbox including its binding