groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GroovyBeginner <groovybegin...@gmail.com>
Subject Iterate ArrayList over XML
Date Fri, 12 Aug 2016 18:13:48 GMT
I am iterating an arraylist over xml and forming another xml with the list of
values present in arraylist. Here is the code am using

import groovy.xml.MarkupBuilder

def books='''
    <response version-api="2.0">
        <value>
             2 
            *3* 
            <e>4</e> 
       </value>
    </response>'''

def lst=["a","b","c","d"]

def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
xml.langs {
for(String str: lst)
{
def response = new XmlSlurper().parseText(books)
       
      response.value.'*'.find{ node->        
       if(node.name() == str)
       {
       "${str}"(node)
       }       
       }
}
}
println sw

which is giving the output correctly. How ever it fails to pull out if any
of the node has child elements

def books='''
    <response version-api="2.0">
        <value>
             2 
            *
              <e>4</e> 
              *
               <g>123</g>
       </value>
    </response>'''
 
The output should be  

<response version-api="2.0">
        <value>
             2 
            *
              <e>4</e> 
              *
              </value>
    </response>
Is there any better approach to do this



--
View this message in context: http://groovy.329449.n5.nabble.com/Iterate-ArrayList-over-XML-tp5734617.html
Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message