groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Iterate ArrayList over XML Groovy
Date Sun, 14 Aug 2016 08:25:41 GMT
There are numerous ways to manipulate XML depending on exactly what
you are wanting to achieve. The following might be useful:

def xml = '''\
<response version-api="2.0">
    <value>
        <a>2</a>
        <b>
            <e>4</e>
        </b>
        <g>123</g>
   </value>
</response>'''
def list = ['response', 'value', 'a', 'b', 'e']
def root = new XmlParser().parseText(xml)
root.depthFirst().each {
  if (!(it.name() in list)) it.'..'.remove(it)
}
groovy.xml.XmlUtil.serialize(root)

Cheers, Paul.


On Sun, Aug 14, 2016 at 12:51 PM, GroovyBeginner
<groovybeginner@gmail.com> wrote:
> I am iterating an arraylist over xml and creating another xml with the list
> of values present in arraylist. Here is the code am using
>
>
> This code is working fine for the xml which does not have child nodes How to
> get all the child nodes of a parent which is present in the arraylist. For
> example if my input is
>
>
>
> the corresponding xml output should be
>
>
>
>
> Since a and b are present in the list. Kindly suggest the approach to handle
> this scenario
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Iterate-ArrayList-over-XML-Groovy-tp5734628.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message