0

I am having trouble sorting my XML with Groovy. I understand how to do it using the attribute but I want to sort using the value of a node.

For instance I want to sort this XML by the StartDate :

<GetActivitiesByDatesResponse>
    <GetActivitiesByDatesResult>
        <Event>
            <EventCode>1244</EventCode>
            <Name>Event1</Name>
            <StartDate>2015-08-13T10:00:00</StartDate>
        </Event>
        <Event>
            <EventCode>1245</EventCode>
            <Name>Event2</Name>
            <StartDate>2015-07-15T10:00:00</StartDate>
        </Event>
        <Event>
            <EventCode>1246</EventCode>
            <Name>Event3</Name>
            <StartDate>2015-07-30T10:00:00</StartDate>
        </Event>
    </GetActivitiesByDatesResult>
</GetActivitiesByDatesResponse>

Here's my code :

root = new XmlSlurper(false,false).parseText(payload)

if(root.name() == 'GetActivitiesResponse' || root.name() == 'GetActivitiesByDatesResponse')
{
    startEach = root.children().children()
}
else
{
    startEach = root.children()    
}
startEach.sort(true) {it.StartDate}


def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
        mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
                 escapeAttributes = false  
                 getPrinter().setAutoIndent(false);  
                 "newroot"() {
                    startEach.each { p -> "w_evenement"() {
                        ///allmylogic
                    }
                }
    }
    w.toString().replaceAll(">\\s+<", "><").trim();
}

My payload is the XML i posted higher this is because it comes from MuleESB but just assume it is a string.

Thanks for the help.

Paperbag Writer
  • 823
  • 1
  • 11
  • 39

1 Answers1

4

You could do this for the given example XML:

def xml = '''<GetActivitiesByDatesResponse>
    <GetActivitiesByDatesResult>
        <Event>
            <EventCode>1244</EventCode>
            <Name>Event1</Name>
            <StartDate>2015-08-13T10:00:00</StartDate>
        </Event>
        <Event>
            <EventCode>1245</EventCode>
            <Name>Event2</Name>
            <StartDate>2015-07-15T10:00:00</StartDate>
        </Event>
        <Event>
            <EventCode>1246</EventCode>
            <Name>Event3</Name>
            <StartDate>2015-07-30T10:00:00</StartDate>
        </Event>
    </GetActivitiesByDatesResult>
</GetActivitiesByDatesResponse>'''

import groovy.xml.*

def x = new XmlParser().parseText(xml)

println XmlUtil.serialize(new StreamingMarkupBuilder().bind {
    GetActivitiesByDatesResponse {
        GetActivitiesByDatesResult {
            x.GetActivitiesByDatesResult[0]
             .children()
             .sort { Date.parse("yyyy-MM-dd'T'HH:mm:ss", it.StartDate.text()) }
             .each { e ->
                Event {
                    EventCode(e.EventCode.text())
                    Name(e.Name.text())
                    StartDate(e.StartDate.text())
                }
            }
        }
    }
})
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thank you so much! Adding `.sort { Date.parse("yyyy-MM-dd'T'HH:mm:ss", it.StartDate.text()) }` as such : `startEach.sort { Date.parse("yyyy-MM-dd'T'HH:mm:ss", it.StartDate.text()) }.each { p -> "w_evenement"()` did the trick and didn't have to change my code at all! – Paperbag Writer Nov 12 '15 at 16:09