[lxml-dev] Feature request / patch: Copy a node
I do have to make copies of nodes in an application I'm writing, so I've tried making a patch for lxml to allow me to do that. The patch is attached, but since I don't have experience with Pyrex and libxml2 there could well be something wrong with it. It seems to work, though. Also I don't know if creating a new document / _ElementTree for the new nodes is the right thing to do, but I suppose it should be... Regards Florian Index: src/lxml/etree.pyx =================================================================== --- src/lxml/etree.pyx (revision 12556) +++ src/lxml/etree.pyx (working copy) @@ -370,7 +370,21 @@ c_node = c_node.next else: raise ValueError, "Matching element could not be found" + + def copy(self, recursive = True): + # 1 = copy recursive; 2 = don't copy recursive + recursive = int(not recursive) + 1 + + cdef xmlNode* c_node + cdef xmlDoc* c_doc + c_doc = theParser.newDoc() + etree = _elementTreeFactory(c_doc) + c_node = tree.xmlDocCopyNode(self._c_node, c_doc, recursive) + tree.xmlDocSetRootElement(c_doc, c_node) + + return _elementFactory(etree, c_node) + # PROPERTIES property tag: def __get__(self):
Florian Wagner wrote:
I do have to make copies of nodes in an application I'm writing, so I've tried making a patch for lxml to allow me to do that. The patch is attached, but since I don't have experience with Pyrex and libxml2 there could well be something wrong with it. It seems to work, though.
Great, a patch, thanks! I've contacted Fredrik Lundh about this too, as ElementTree seems to be lacking this operation as well.
Also I don't know if creating a new document / _ElementTree for the new nodes is the right thing to do, but I suppose it should be...
I'll make sure to study this patch carefully and give you feedback on it. Thanks again! Martijn
Martijn Faassen wrote:
Florian Wagner wrote:
I do have to make copies of nodes in an application I'm writing, so I've tried making a patch for lxml to allow me to do that. The patch is attached, but since I don't have experience with Pyrex and libxml2 there could well be something wrong with it. It seems to work, though.
Great, a patch, thanks!
I've contacted Fredrik Lundh about this too, as ElementTree seems to be lacking this operation as well.
Fredrik has now replied -- he says ElementTree supports the copy module (copy.copy and copy.deepcopy). I'll look into supporting a copy operation this way, being informed by your patch. :) That is, deepcopy, as shallow copies don't really make sense in the context of libxml2. Regards, Martijn
Martijn Faassen wrote:
Martijn Faassen wrote:
Florian Wagner wrote:
I do have to make copies of nodes in an application I'm writing, so I've tried making a patch for lxml to allow me to do that. The patch is attached, but since I don't have experience with Pyrex and libxml2 there could well be something wrong with it. It seems to work, though.
Great, a patch, thanks!
I've contacted Fredrik Lundh about this too, as ElementTree seems to be lacking this operation as well.
Fredrik has now replied -- he says ElementTree supports the copy module (copy.copy and copy.deepcopy). I'll look into supporting a copy operation this way, being informed by your patch. :) That is, deepcopy, as shallow copies don't really make sense in the context of libxml2.
A while back I worked on copy.deepcopy() support for Elements, but I forgot to check it in. A basic implementation has now been added. Please try it out if you like. Regards, Martijn
participants (2)
-
Florian Wagner
-
Martijn Faassen