You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Manish Yadav (JIRA)" <ji...@apache.org> on 2017/05/26 02:42:05 UTC

[jira] [Commented] (GROOVY-5682) Add clone or copy constructor to XML Node

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

Manish Yadav commented on GROOVY-5682:
--------------------------------------

Hi,

Thanks for adding clone method [~paulk]. It helped me create my own custom methods as we are using very old version.

Also, as of now groovy supports clone node feature without cloning parents.
We can add following custom methods in some util class to make cloning more advanced :
1:
private Node cloneNode(Node node, Node parentNode){
Object newValue = node.value()
Node clonedNode = new Node(null, node.name(), new HashMap(node.attributes()), newValue)
if (newValue instanceof NodeList)
{ NodeList nodes = (NodeList) newValue newValue = cloneNodeList(nodes, clonedNode) }
clonedNode.setValue(newValue)
clonedNode.setParent(parentNode)
return clonedNode
}
2:
private NodeList cloneNodeList(NodeList nodeList, Node parentNode) {
NodeList result = new NodeList(nodeList.size())
for (int i = 0; i < nodeList.size(); i++) {
Object next = nodeList.get
if (next instanceof Node)
{ result.add(cloneNode((Node) next, parentNode)) }
else
{ result.add(next) }
}
return result
} 

> Add clone or copy constructor to XML Node
> -----------------------------------------
>
>                 Key: GROOVY-5682
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5682
>             Project: Groovy
>          Issue Type: New Feature
>    Affects Versions: 2.0.1
>            Reporter: Marcel Szalbach
>            Assignee: Paul King
>            Priority: Minor
>             Fix For: 2.0.2, 2.1.0-beta-1
>
>
> When retrieving an node from an XML there is no easy way to clone this node for further processing without changing the original XML.
> Currently I used a workaround:
> {code:java}
> def clonedNode = new XmlParser().parseText( XmlUtil.serialize( xmlNode ) )
> {code}
> It would be a lot easier if the Node object have a copy constructor or clone would be supported.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)