python & lxml: adding empty element into an etree
How can I add an empty element like <br/>
to an ElementTree in Python?
开发者_StackOverflow社区Thanks!
The same way you would add a non-empty element.
>>> from lxml import etree
>>> f = etree.fromstring('<document></document>')
>>> ele = etree.Element('br') # the element to be inserted
>>> f.insert(0, ele)
>>> print etree.tostring(f, pretty_print=True)
<document>
<br/>
</document>
精彩评论