How to clone Element
objects in Python xml.etree
? I'm trying to procedurally move and copy (then modify their attributes) nodes.
lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.
etree. ElementTree module and Minidom (Minimal DOM Implementation). Parsing means to read information from a file and split it into pieces by identifying parts of that particular XML file.
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:
new_element = lxml.etree.fromstring(lxml.etree.tostring(elem))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With