Discussion:
[groovy-user] GPathResult : Removing XML node
Tridib Samanta
2011-06-24 21:26:37 UTC
Permalink
Hi Experts,

I am frustrated after 3-4 hours of effort and failed to remove a xml node. :(.
Is  not there a simple remove() method to remove xml node???

I using the following code to remove all empty nodes from xml:

 void cleanUpNode(GPathResult node) {
        if(node) {
            GPathResult children = node.children()
            children.each { child->
                cleanUpNode(child)
            }
           
            def childrenCount = node.children().size()
            if(!value) {
                if(childrenCount == 0) {
                   node.parent().remove(node) --->>. This does not work
                }
            }
        }
    }

Please HELP!


Thanks
Tridib
Paulo Gabriel Poiati
2011-06-24 22:19:48 UTC
Permalink
Hello Tribid,

I think it's easy to use XmlParser for this task.

EX:

final def EXAMPLE = """
<foo>
<bar>Bar</bar>
<baz></baz>
<xpto></xpto>
<qux>
<zoo>123</zoo>
<boo></boo>
</qux>
</foo>
"""

def records = new XmlParser().parseText EXAMPLE

def cleanUpNode( node ) {
println node
def childs = node.children()
def remove = []
childs.each {
if ( it instanceof Node ) {
if ( !it.children() ) {
remove.add it
}
cleanUpNode it
}
}

remove.each { node.remove( it ) }
}

cleanUpNode records

new XmlNodePrinter( preserveWhitespace: true ).print( records )

I added the nodes to a collection before removing because of a
ConcurrentModificationException that would occur.

[]'s
Paulo Poiati

blog.paulopoiati.com
Post by Tridib Samanta
Hi Experts,
I am frustrated after 3-4 hours of effort and failed to remove a xml node.
:(. Is not there a simple remove() method to remove xml node???
void cleanUpNode(GPathResult node) {
if(node) {
GPathResult children = node.children()
children.each { child->
cleanUpNode(child)
}
def childrenCount = node.children().size()
if(!value) {
if(childrenCount == 0) {
*node.parent().remove(node) --->>. This does not work*
}
}
}
}
Please HELP!
Thanks
Tridib
Paul King
2011-06-25 02:45:06 UTC
Permalink
Yes, if you want to use XmlSlurper, the deletes are lazy,
i.e. don't come into effect until you write the whole thing
back out to a stream or wherever.

Cheers, Paul.

On Sat, Jun 25, 2011 at 8:19 AM, Paulo Gabriel Poiati
Post by Paulo Gabriel Poiati
Hello Tribid,
I think it's easy to use XmlParser for this task.
final def EXAMPLE = """
<foo>
    <bar>Bar</bar>
    <baz></baz>
    <xpto></xpto>
    <qux>
        <zoo>123</zoo>
        <boo></boo>
    </qux>
</foo>
"""
def records = new XmlParser().parseText EXAMPLE
def cleanUpNode( node ) {
    println node
    def childs = node.children()
    def remove = []
    childs.each {
        if ( it instanceof Node ) {
            if ( !it.children() ) {
                remove.add it
            }
            cleanUpNode it
        }
    }
    remove.each { node.remove( it ) }
}
cleanUpNode records
new XmlNodePrinter( preserveWhitespace: true ).print( records )
I added the nodes to a collection before removing because of a
ConcurrentModificationException that would occur.
[]'s
Paulo Poiati
blog.paulopoiati.com
Post by Tridib Samanta
Hi Experts,
I am frustrated after 3-4 hours of effort and failed to remove a xml node.
:(. Is  not there a simple remove() method to remove xml node???
 void cleanUpNode(GPathResult node) {
        if(node) {
            GPathResult children = node.children()
            children.each { child->
                cleanUpNode(child)
            }
            def childrenCount = node.children().size()
            if(!value) {
                if(childrenCount == 0) {
                   node.parent().remove(node) --->>. This does not work
                }
            }
        }
    }
Please HELP!
Thanks
Tridib
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Tridib Samanta
2011-06-25 06:51:17 UTC
Permalink
Thanks Paulo! The remove of XmlParser is working.
But, for an xml like bellow after first run, we end up with enother empty node
<qux/>. Only the leaf empty nodes were getting removed.
final def EXAMPLE = """
<foo>
    <bar>Bar</bar>
    <baz></baz>
    <xpto></xpto>
    <qux>
        <boo></boo>
    </qux>
</foo>
"""
I modified it as follows:

final def EXAMPLE = """
<foo>
    <bar>Bar</bar>
    <baz></baz>
    <xpto></xpto>
    <qux>
        <boo></boo>
    </qux>
</foo>
"""
def records = new XmlParser().parseText (EXAMPLE)
def cleanUpNode( node ) {
    println node
    def childs = node.children()
   
    def remove = []
    childs.each {
        if (it instanceof Node) {
           
            if ( !it.children()) {               
                remove.add it  
            } else {
                cleanUpNode it
                if(!it.children()) {
                    remove.add it
                }
            }
        }
    }
   
    remove.each { node.remove( it ) }
}
cleanUpNode records
new XmlNodePrinter( preserveWhitespace: true ).print( records )


Cheers!
Tridib



________________________________
From: Paulo Gabriel Poiati <paulogpoiati-***@public.gmane.org>
To: user-i9PBDF1N6cxnkHa44VUL00B+***@public.gmane.org
Sent: Fri, June 24, 2011 3:19:48 PM
Subject: Re: [groovy-user] GPathResult : Removing XML node

Hello Tribid, 

I think it's easy to use XmlParser for this task.

EX:

final def EXAMPLE = """
<foo>
    <bar>Bar</bar>
    <baz></baz>
    <xpto></xpto>
    <qux>
        <zoo>123</zoo>
        <boo></boo>
    </qux>
</foo>
"""

def records = new XmlParser().parseText EXAMPLE

def cleanUpNode( node ) {
    println node
    def childs = node.children()
    def remove = []
    childs.each {
        if ( it instanceof Node ) {
            if ( !it.children() ) {
                remove.add it   
            }
            cleanUpNode it
        }
    }
    
    remove.each { node.remove( it ) }
}

cleanUpNode records

new XmlNodePrinter( preserveWhitespace: true ).print( records )
​I added the nodes to a collection before removing because of a
ConcurrentModificationException that would occur.


[]'s
Paulo Poiati

blog.paulopoiati.com



On Fri, Jun 24, 2011 at 6:26 PM, Tridib Samanta <***@yahoo.com> wrote:

Hi Experts,
Post by Tridib Samanta
I am frustrated after 3-4 hours of effort and failed to remove a xml node. :(.
Is  not there a simple remove() method to remove xml node???
 void cleanUpNode(GPathResult node) {
        if(node) {
            GPathResult children = node.children()
            children.each { child->
                cleanUpNode(child)
            }
           
            def childrenCount = node.children().size()
            if(!value) {
                if(childrenCount == 0) {
                   node.parent().remove(node) --->>. This does not work
                }
            }
        }
    }
Please HELP!
Thanks
Tridib
Loading...