fork download
  1. //Define your data as list of maps as shown below
  2. def data = [
  3. [number: 26354, price: 15000, date: '17-12-2017', customer: 'Clinton'],
  4. [number: 16514, price: 28000, date: '24-08-2017', customer: 'Mark']
  5. ]
  6.  
  7. def xml = new groovy.xml.StreamingMarkupBuilder().bind {
  8. //Change the root element as needed instead of invoiceRequest
  9. invoiceRequest {
  10. invoices {
  11. //Loop thru list and create invoice elements
  12. data.each { inv ->
  13. invoice (number: inv.number) {
  14. price (inv.price as double)
  15. date (inv.date)
  16. customer(inv.customer)
  17. }
  18. }
  19. }
  20. }
  21. }
  22. println groovy.xml.XmlUtil.serialize(xml)
Success #stdin #stdout 2.45s 2914304KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="UTF-8"?><invoiceRequest>
  <invoices>
    <invoice number="26354">
      <price>15000.0</price>
      <date>17-12-2017</date>
      <customer>Clinton</customer>
    </invoice>
    <invoice number="16514">
      <price>28000.0</price>
      <date>24-08-2017</date>
      <customer>Mark</customer>
    </invoice>
  </invoices>
</invoiceRequest>