在Groovy中遍历每个XML员工记录
我的XML看起来像是在Groovy中遍历每个XML员工记录,groovy,Groovy,我的XML看起来像是 <?xml version="1.0" encoding="UTF-8"?> <TimeAccount> <TimeAccount> <accountType>CMSV</accountType> <bookingEndDate>2021-12-31T00:00:00.000</bookingEndDate> <use
<?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>
abc111
),如何为每个员工节点执行此操作
bookingAmount
)子节点,其预订类型为EMPLOYEE\u TIME
,并在员工头级别创建如上所述的节点,我可以为一条EMPLOYEE\u TIME
记录(如上所述)执行此操作,但无法递归添加所有子节点。如何实现
def xml=new XmlParser().parseText(“”)
CMSV
2021-12-31T00:00:00.000
36218
CMSV
2021-12-31T00:00:00.000
abc222
托斯
2021-12-31T00:00:00.000
abc111
176
手动调整
-8
雇员时间
-8
雇员时间
-8
雇员时间
托斯
2021-12-31T00:00:00.000
abc222
256
手动调整
-8
雇员时间
-8
雇员时间
2021-01-01T00:00:00.000
供货商资质评估及最终选择权限
2021-12-31T00:00:00.000
abc111
40
手动调整
-8
雇员时间
-8
雇员时间
2021-01-01T00:00:00.000
供货商资质评估及最终选择权限
2021-12-31T00:00:00.000
abc222
40
手动调整
''')
xml.TimeAccount.findAll{it.accountType.text()=='PTOS'}。每个{ptoNode->
ptoNode.timeAccountDetails.timeAccountDetails.find{it.bookingType.text()
ptosHours=detailNode.bookingAmount.text()
ptoNode.appendNode('newPTONode',null,ptosHours)
}
}
println groovy.xml.XmlUtil.serialize(xml)
使用findAll{…}
而不是find{…}
感谢您的快速回复。当我使用findAll代替find java.lang.NoSuchMethodException时,我得到以下错误:没有方法签名:java.util.ArrayList.text()适用于参数类型:()值:[]可能的解决方案:get(int)、get(int)、set(int、java.lang.Object)、set(int、java.lang.Object)、getAt(int)、getAt(java.util.Collection)是否可以在java循环(头节点)中编写一个.each and then if(condition)like in loop{if(condition)//accoutType==PTOS循环(子节点){if(condition)//booking type==EMPLOYEE_TIME}在groovy中是可以的?如果可以,您能分享一下吗?找到“问题已回答”按钮以获得下一个答案;)我仍然看不到上一个问题上的“问题已回答”按钮:)对不起。可能在本次讨论中它会显示:)谢谢@daggett,这很好。使用“?”有什么用?。使用“代替”.each”?我将标记此问题的简短回答。getX()。使用{x->x.f1()}
与x=getX();x.f1();
,getX()?.使用{x->x.f1()}
与x=getX();如果(x){x.f1();}
使用:
安全访问:
非常感谢。
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() )
}
}
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)