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.