首页 > 解决方案 > 遍历 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() )
     }
     }
  1. 以上代码仅适用于第一个员工(abc111),如何为每个员工节点执行此操作?

  2. 我需要在员工节点中添加bookingAmount具有预订类型EMPLOYEE_TIME的所有小时 () 子节点,并在员工标题级别创建类似上面的节点,我可以为一条EMPLOYEE_TIME记录执行此操作(与上面相同),但无法递归添加所有子节点。如何实现呢?

  3. 如何在 groovy 脚本中循环遍历高级和详细级别的 XML 文档?

标签: groovy

解决方案


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)


推荐阅读