lxml.etree._Element
Table of Contents
- References
- Create a new element
- Clone an element
- Test if an element has children
- Add a new element before/after an element
- Remove all children
- Remove an element
- Test Element Equality
- Surround an element with another tag
- Add a child element at very first
References
Create a new element howto
elem.append(etree.Element("child1"))
child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")
etree.Element()
is not the class itself. It's Element factory. To create an element but not the etree._Element
, such as HtmlElement
, create it manually:
Clone an element howto
- Use
deepcopy
Test if an element has children howto
Add a new element before/after an element howto
Remove all children howto
Remove an element howto
Test Element Equality howto
def elem_equal(e1, e2):
return (e1.tag == e2.tag and
e1.text == e2.text and
e1.tail == e2.tail and
e1.attrib == e2.attrib and
len(e1) == len(e2))
return all(elem_equal(c1, c2) for c1, c2 in zip(e1, e2))
Surround an element with another tag howto
You should:
- Clone the element
- Create a new element and add the cloned one as a child
- Add the new element previous or next to the original element
- Remove the original one
from copy import deepcopy
from lxml.html.builder import DIV
def surround(elem):
clone = deeopcopy(elem)
div = DIV(clone)
elem.addprevious(div)
elem.getparent().remove(elem)
return div
Add a child element at very first howto
As element.text
is always placed before the first subelement, we should move element.text
to subelement.tail
: