You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by GroovyBeginner <gr...@gmail.com> on 2016/08/12 18:13:48 UTC

Iterate ArrayList over XML

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.