开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜