似乎
<STATUS>标签已分配了的
text属性
None:
>>> tree[0]<Element STATUS at 0x11708d4d0>>>> tree[0].text>>> tree[0].text is NoneTrue
如果
text将
<STATUS>标记的属性设置为空字符串,则应获得所需的内容:
>>> tree[0].text = ''>>> etree.tostring(tree)'<ERROR>The status is <STATUS></STATUS>.</ERROR>'
考虑到这一点,您可能可以
text在编写XML之前遍历DOM树并修复属性。像这样:
# prevent creation of self-closing tagsfor node in tree.iter(): if node.text is None: node.text = ''



