开发者

python & lxml: adding empty element into an etree

开发者 https://www.devze.com 2023-02-12 16:12 出处:网络
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.

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消