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/24 14:51:35 UTC

Delete Sub Child XML Nodes

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.

Fwd: Delete Sub Child XML Nodes

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

On Thu, Aug 25, 2016 at 12:51 AM, GroovyBeginner
<gr...@gmail.com> wrote:
> 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.