[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [xml] Copying XML subtrees across documents (in Python)



Jos Vos wrote:
> What is the recommended way (in the libxml2 Python binding) to copy
> a node and all of its children (but not its siblings!) from one
> document to another document?

;) the recommended way to use libxml2 from Python is lxml.


> I tried (x is a node in the new document, y a node in the old document):
> 	
> 	x.addChild(y)
> 	x.addChild(y.copyNodeList())

Ok, you want to copy, not move the node, so lxml.etree would do:

   >>> from copy import deepcopy
   >>> x.append(deepcopy(y))

Short and intuitive.

Stefan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]