You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Paul King <pa...@asert.com.au> on 2016/08/24 23:06:07 UTC

Fwd: Delete Sub Child XML Nodes

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 <gr...@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.

Re: Delete Sub Child XML Nodes

Posted by Paul King <pa...@asert.com.au>.
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 <pa...@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 <gr...@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.