在 Groovy 脚本中按日期字段对 XML 个子节点进行排序

Sort XML child nodes by date field in Groovy Script

我的 XML 如下所示...我需要按 startDate 元素对 jobInfoNav 下的 EmpJob 节点进行排序。我尝试了下面的代码并遵循了下面的日志,它没有给出任何错误但列表没有排序。请帮忙。

我在 SAP CPI 中使用这个 groovy 脚本。

XML

<PerPerson>
<PerPerson>
  <employmentNav>
    <EmpEmployment>
      <jobInfoNav>
        <EmpJob>
          <event>10193</event>
          <userId>abc123</userId>
          <eventReason>SP</eventReason>
          <startDate>2020-09-20T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10196</event>
          <userId>abc123</userId>
          <eventReason>RP</eventReason>
          <startDate>2019-04-14T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10186</event>
          <userId>abc123</userId>
          <eventReason>RL</eventReason>
          <startDate>2019-08-19T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10203</event>
          <userId>abc123</userId>
          <eventReason>HN</eventReason>
          <startDate>2018-05-02T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10193</event>
          <userId>abc123</userId>
          <eventReason>ZC</eventReason>
          <startDate>2020-03-08T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10193</event>
          <userId>abc123</userId>
          <eventReason>EC</eventReason>
          <startDate>2018-09-23T00:00:00.000</startDate>
        </EmpJob>
      </jobInfoNav>
    </EmpEmployment>
  </employmentNav>
</PerPerson>
<PerPerson>
  <employmentNav>
    <EmpEmployment>
      <jobInfoNav>
        <EmpJob>
          <event>10203</event>
          <userId>abc789</userId>
          <eventReason>HN</eventReason>
          <startDate>2020-06-22T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10193</event>
          <userId>abc789</userId>
          <eventReason>ZC</eventReason>
          <startDate>2020-06-22T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10180</event>
          <userId>abc789</userId>
          <eventReason>LD</eventReason>
          <startDate>2020-06-24T00:00:00.000</startDate>
        </EmpJob>
      </jobInfoNav>
    </EmpEmployment>
  </employmentNav>
</PerPerson>
<PerPerson>
  <employmentNav>
    <EmpEmployment>
      <jobInfoNav>
        <EmpJob>
          <event>10193</event>
          <userId>abc999</userId>
          <eventReason>SP</eventReason>
          <startDate>2020-08-30T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10206</event>
          <userId>abc999</userId>
          <eventReason>RN</eventReason>
          <startDate>2020-08-24T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10203</event>
          <userId>abc999</userId>
          <eventReason>HC</eventReason>
          <startDate>2020-01-21T00:00:00.000</startDate>
        </EmpJob>
      </jobInfoNav>
    </EmpEmployment>
  </employmentNav>

groovy 脚本

   def xml =  new XmlSlurper().parseText(body)     //use of of these depending on which code you are using
   def xml =  new XmlParser().parseText(body)   //use of of these depending on which code you are using

     XmlUtil.serialize(new StreamingMarkupBuilder().bind {                                           
PerPerson{
    PerPerson {
        employmentNav{
            EmpEmployment{
                jobInfoNav{
                     xml.jobInfoNav[0]
                     .children()
                     .sort { Date.parse("yyyy-MM-dd'T'HH:mm:ss.sss", it.startDate.text()) }
                     .each { e ->
                         EmpJob {
                                event(e.event.text())
                                userId(e.userId.text())
                                eventReason(e.eventReason.text())
                                startDate(e.startDate.text())
                                }     
                                        
                            }
                        }
   
                     }
                 }
             }
         }
    }
    )
 
    message.setBody(XmlUtil.serialize(xml))

下面也试过了

// xml.PerPerson.employmentNav.EmpEmployment.jobInfoNav.EmpJob.sort(true) {it.attribute('startDate')} 

也试过下面的代码

 new StreamingMarkupBuilder().bind {                                                                    // not working
    xml.PerPerson.PerPerson.EmpEmploymentNav.EmpEmployment.jobInfoNav[0].children().sort { Date.parse("yyyy-MM-dd'T'HH:mm:ss.sss", it.startDate.text()) }
                     .each { e ->
                         EmpJob {
                                event(e.event.text())
                                userId(e.userId.text())
                                eventReason(e.eventReason.text())
                                startDate(e.startDate.text())
                                }
                            }
                        }

我没有看到任何错误,但 EmpJob 节点未排序。请帮忙。

已关注在线话题

https://groups.google.com/forum/#!topic/groovy-user/rNapYFuFl-0

https://community.smartbear.com/t5/API-Functional-Security-Testing/Sorting-XML-response-before-assertion/td-p/107186

Use Groovy to Sort XML File

Sorting XML in Groovy

def body='''
<PerPerson>
  <employmentNav>
    <EmpEmployment>
      <jobInfoNav>
        <EmpJob>
          <event>10206</event>
          <userId>abc123</userId>
          <eventReason>RH</eventReason>
          <startDate>2020-10-05T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10186</event>
          <userId>abc123</userId>
          <eventReason>RL</eventReason>
          <startDate>2020-03-11T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10203</event>
          <userId>abc123</userId>
          <eventReason>HN</eventReason>
          <startDate>2019-12-16T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10190</event>
          <userId>abc123</userId>
          <eventReason>Z1</eventReason>
          <startDate>2020-06-13T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10193</event>
          <userId>abc123</userId>
          <eventReason>ZC</eventReason>
          <startDate>2020-03-11T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10189</event>
          <userId>abc123</userId>
          <eventReason>RW</eventReason>
          <startDate>2020-06-13T00:00:00.000</startDate>
        </EmpJob>
        <EmpJob>
          <event>10180</event>
          <userId>abc123</userId>
          <eventReason>FU</eventReason>
          <startDate>2020-04-15T00:00:00.000</startDate>
        </EmpJob>
      </jobInfoNav>
    </EmpEmployment>
  </employmentNav>
</PerPerson>
'''

def xml =  new XmlParser().parseText(body)
def jobInfoNav = xml.employmentNav.EmpEmployment.jobInfoNav[0]
//get value - array of child nodes, sort, and set it back
jobInfoNav.value = jobInfoNav.value().sort{ e-> e.startDate.text() }

groovy.xml.XmlUtil.serialize(xml)