Skip to content

Instantly share code, notes, and snippets.

@robvalk
Created December 1, 2014 04:24
Show Gist options
  • Save robvalk/90bd7c1294948c2c4f4d to your computer and use it in GitHub Desktop.
Save robvalk/90bd7c1294948c2c4f4d to your computer and use it in GitHub Desktop.
import org.mule.api.transport.OutputHandler
// store the current payload at the time the script is invoked
// (i.e. the back-end response stream/iterator)
def itr = payload;
// return a Groovy closure / lambda
return {evt, out ->
// define a stream XML builder object
def xml = new groovy.xml.StreamingMarkupBuilder();
// construct the builder function
def xmlClosure = {
mkp.declareNamespace(ns0: "http://mycomany.com/schema/account/v1/xsd")
is.accountList { // XML root element
itr.each { _acnt -> // lazily iterate over the back-end response records
account { // XML record element
systemId(_acnt.accountId)
// map other fields in here as necessary
// ...
}
}
}
}
// write the resulting XML directly to the output stream provided by Mule as a parameter
out << xml.bind(xmlClosure);
} as OutputHandler // Groovy way to implement a Java interface with a single method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment