groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mauro Molinari (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-1832) XMLParser: remove method on NodeList returns true (success). But, Node is not removed.
Date Wed, 28 Feb 2018 15:54:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-1832?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16380451#comment-16380451
] 

Mauro Molinari edited comment on GROOVY-1832 at 2/28/18 3:53 PM:
-----------------------------------------------------------------

Hi Paul,
 I find the current Groovy behaviour crazy... I'm using {{XmlParser}} because I need to parse
and manipulate a XML file, then save it elsewhere, so the page at [http://groovy-lang.org/processing-xml.html]
suggests it's better to use {{XmlParser}} rather than {{XmlSluper}} (which would require a
reparsing after each change - at least this is what I understand).

I'm traversing the tree by iterating over children of a given node, so I am iterating over
NodeLists a lot. Neither of these seem to work to actually delete nodes from he XML tree:
 * Iterator.remove
 * Collection.removeAll(Closure)
 * Collection.findAll(Closure) followed by Collection.removeAll(Collection)

Indeed, the above do delete nodes from the NodeList they are invoked on, but, as you say,
they don't remove them from the tree. After a lot of trial and error, I got it working by
invoking {{nodeToRemove.parent().remove(nodeToRemove)}} for the nodes I want to delete (although,
on a first attempt, this didn't work either, I don't know why)

No error is reported by the remove methods above, simply nothing is done, the XML tree still
has the node. I think this is really counter intuitive.

By the way, the page at [http://groovy-lang.org/processing-xml.html#_modifying_removing_nodes]
is supposed to show examples for modifying/removing nodes, but no remove example is shown.


was (Author: mauromol):
Hi Paul,
I find the current Groovy behaviour crazy... I'm using {{XmlParser}} because I need to parse
and manipulate an XML file, then save it elsewhere, so the page at [http://groovy-lang.org/processing-xml.html]
suggests it's better to use {{XmlParser}} rather than {{XmlSluper}} (which would require a
reparsing after each change - at least this is what I understand).

I'm traversing the tree by iterating over children of a given node, so I am iterating over
NodeLists a lot. Neither of these seem to work to actually delete nodes from he XML tree:
 * Iterator.remove
 * Collection.removeAll(Closure)
 * nodeToRemove.parent().remove(nodeToRemove) (once nodeToRemove has been found by iteration
over the NodeList)

No errors, simply nothing is done, the XML tree still has the node.

Now I'm trying the "findAll" way, but I really think this is broken.

By the way, the page at [http://groovy-lang.org/processing-xml.html#_modifying_removing_nodes]
is supposed to show examples for modifying/removing nodes, but no remove example is shown.

> XMLParser: remove method on NodeList returns true (success).  But, Node is not removed.
> ---------------------------------------------------------------------------------------
>
>                 Key: GROOVY-1832
>                 URL: https://issues.apache.org/jira/browse/GROOVY-1832
>             Project: Groovy
>          Issue Type: Improvement
>          Components: XML Processing
>    Affects Versions: 1.0
>         Environment: Windows XP.    Eclipse 3.2.
>            Reporter: Ricky Gomez
>            Assignee: Paul King
>            Priority: Minor
>             Fix For: 1.1-rc-1
>
>         Attachments: XmlParserRemoveTest.groovy
>
>
> Using XMLParser:
> node.nodelist.remove(o)  returns true.   But node is not removed.
> I think the node.nodelist.remove(o) syntax is preferred to o.parent().value.remove(o)
which does work.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message