How to clone Element
objects in Python xml.etree
? I'm trying to procedurally move and copy (then modify their attributes) nodes.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
At least in Python 2.7 etree Element has a copy method: http://hg.python.org/cpython/file/2.7/Lib/xml/etree/ElementTree.py#l233
It is a shallow copy, but that is preferable in some cases.
In my case I am duplicating some SVG Elements and adding a transform. Duplicating children wouldn't serve any purpose since where relevant they already inherit their parent's transform.
You can just use copy.deepcopy() to make a copy of the element. (this will also work with lxml by the way).
A different, and somewhat disturbing solution:
For future reference.
Simplest way to copy a node (or tree) and keep it's children, without having to import ANOTHER library ONLY for that:
If you have a handle on the
Element
elem
'sparent
you can callOtherwise you might want to try
but this is not advised.