lxml convert element to elementtree
Solution 1:
To get the root tree from an _Element
(generated with lxml.html.fromstring
), you can use the getroottree
method:
doc = lxml.html.parse(s)
tree = doc.getroottree()
Solution 2:
The etree.fromstring
method parses an XML string and returns a root element. The etree.ElementTree
class is a tree wrapper around an element and as such requires an element for instantiation.
Therefore, passing the root element to the etree.ElementTree()
constructor should give you what you want:
root = etree.fromstring(s)
nt = etree.ElementTree(root)