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

Accessing the Subnodes of the nodes under parent using groovy

$
0
0

I am novice to groovy.I am trying to reformat the XML using groovy.I am creating XML node for each date between dates orderstartdate and StartDate. But one of the child node(prod) has subnodes(id and count) and those are not appended properly.I am trying the following piece of code.

`

import java.text.*import groovy.xml.*SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd")String orderstartdate = "2023-10-12T18:32:21Z";Date orderstart = parser.parse( orderstartdate )                  def text = '''<results><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><StartDate>2023-10-16T18:00:00Z</StartDate><prod><id>a-6210q</id><count>17</count></prod><prod><id>a-1110w</id><count>17</count></prod></results>'''def xml = new XmlSlurper().parseText( text )def output = new XmlParser().parseText("<root/>")xml.each { eachXmlNode ->Date startDate = parser.parse( orderstartdate )Date endDate = parser.parse( eachXmlNode.StartDate.text() )Date currentDate = new Date( startDate.time )while( currentDate < endDate ) {Node resultsNode = output.appendNode( new QName("results"), [:] )                       eachXmlNode.children().findAll { child -> child.name() != "StartDate" } .each { child ->                               resultsNode.appendNode( new QName(child.name()), [:], child.text() )                                                                                                                                  }                       resultsNode.appendNode(new QName("Date"), [:], parser.format( currentDate ))                       currentDate = currentDate + 1}         }println( XmlUtil.serialize(output ) )

`

Output that I am getting with above code:`

<results xmlns=""><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><prod>a-6210q17</prod><prod>a-1110w17</prod><Date>2023-10-12</Date></results><results xmlns=""><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><prod>a-6210q17</prod><prod>a-1110w17</prod><Date>2023-10-13</Date></results><results xmlns=""><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><prod>a-6210q17</prod><prod>a-1110w17</prod><Date>2023-10-14</Date></results><results xmlns=""><orderid>4ff45676-f77e-430a-ba7d-e02a20303c0d</orderid><prod>a-6210q17</prod><prod>a-1110w17</prod><Date>2023-10-15</Date></results></root>

`

Output that I am expecting:`

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

`

Do you know what needs to be added here to get the right format?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

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