groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Delete Sub Child XML Nodes
Date Wed, 24 Aug 2016 23:17:41 GMT
def xml = new XmlParser().parseText(employees)
xml.'**'.ZipCode.each { it.'..'.remove(it) }
XmlUtil.serialize(xml)

On Thu, Aug 25, 2016 at 9:06 AM, Paul King <paulk@asert.com.au> wrote:
> I forwarded your email to the users list since it isn't about
> development of the language codebase itself but rather about using
> Groovy.
>
> Cheers, Paul.
>
> ---------- Forwarded message ----------
> From: GroovyBeginner <groovybeginner@gmail.com>
> Date: Thu, Aug 25, 2016 at 12:51 AM
> Subject: Delete Sub Child XML Nodes
> To: dev@groovy.incubator.apache.org
>
>
> I am unable to delete the sub child xml Nodes. Here is the input and code am
> trying with
>
> import groovy.xml.*;
> def employees='''<Employees>
>                       <Employee>
>                          <ID>123</ID>
>                          <Name>xyz</Name>
>                          <Addresses>
>                              <Address>
>                                 <Country>USA</Country>
>                                 <ZipCode>40640</ZipCode>
>                              </Address>
>                           </Addresses>
>                       </Employee>
>                       <Employee>
>                          <ID>345</ID>
>                          <Name>abc</Name>
>                          <Addresses>
>                              <Address>
>                                 <Country>CA</Country>
>                                 <ZipCode>50640</ZipCode>
>                              </Address>
>                           </Addresses>
>                       </Employee>
>                     </Employees>'''
> def fields = ['Name','ZipCode']
> def xml = new XmlParser().parseText(employees)
> xml.Employee.each { node ->
>      node.children().reverse().each{
>         if(!fields.contains(it.name())) {
>             node.remove(it)
>         }
>     }
> }
>
> XmlUtil.serialize(xml)
>
> How to delete the node ZipCode from the xml?
>
>
>
> --
> View this message in context:
> http://groovy.329449.n5.nabble.com/Delete-Sub-Child-XML-Nodes-tp5734792.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message