python - 覆盖 lxml 行为以编写 Null 标记的结束和开始元素

标签 python xml lxml

root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')

输出是:

<document>
<test/>
</document

我希望输出为:

<document>
<test>
</test>
</document>

我知道两者是等价的,但有没有办法获得我想要的输出。

最佳答案

tostringmethod 参数设置为html。如:

etree.tostring(root, method="html")

引用:Close a tag with no text in lxml

关于python - 覆盖 lxml 行为以编写 Null 标记的结束和开始元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543399/

相关文章:

逐 block 读取文件时Python不返回所有数据

python - 哪些验证码解决方案可以与 App Engine 一起使用?

xml - xmlns =""是有效的 xml namespace 吗?

c# - 选择最后一个 XML 节点

python - 使用lxml和xpath读取GPX

python - 对 def __init__ 中的 __init__ 方法感到困惑

python - 无法在 Aabqus Explicit 中的 odb 文件中创建新步骤

python - 使用Python解析XML : Keeping text within attribute while deleting tag around it

python - 如何在 python lxml 包中的 XPATH 表达式中设置数组索引

python - 使用 lxml HTML 解析 UTF-8/unicode 字符串