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

xa9qqrwz  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(151)

我的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
How to sort XML with Groovy using value of Node (Date)
Use Groovy to Sort XML File
Sorting XML in Groovy

yeotifhr

yeotifhr1#

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)

相关问题