Quantcast
Channel: Accessing the Subnodes of the nodes under parent using groovy - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by tim_yates for Accessing the Subnodes of the nodes under parent using groovy

$
0
0

So, you can do something like this:

import groovy.xml.MarkupBuilderimport groovy.xml.XmlSlurperimport java.time.ZonedDateTimedef xml = new XmlSlurper().parseText(text)def orderStartDate = ZonedDateTime.parse("2023-10-12T18:32:21Z").toLocalDate()def endDate = ZonedDateTime.parse(xml.StartDate.text()).toLocalDate()def orderId = xml.orderid.text()def writer = new StringWriter()new MarkupBuilder(writer).root {    (orderStartDate..<endDate).each { d ->       results {           orderid(orderId)           date(d.toString())           xml.prod.each { p ->               prod {                   id(p.id.text())                   count(p.count.text())               }           }       }    }}println writer.toString()

So that iterates over a range of LocalDate, and uses MarkupBuilder to create some XML

The output from that script is:

<root><results><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><date>2023-10-12</date><prod><id>a-6210q</id><count>17</count></prod><prod><id>a-1110w</id><count>17</count></prod></results><results><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><date>2023-10-13</date><prod><id>a-6210q</id><count>17</count></prod><prod><id>a-1110w</id><count>17</count></prod></results><results><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><date>2023-10-14</date><prod><id>a-6210q</id><count>17</count></prod><prod><id>a-1110w</id><count>17</count></prod></results><results><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><date>2023-10-15</date><prod><id>a-6210q</id><count>17</count></prod><prod><id>a-1110w</id><count>17</count></prod></results></root>

Which I think is what you wanted?


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>