Keep lxml from creating self-closing tags

It seems like the <STATUS> tag gets assigned a text attribute of None:

>>> tree[0]
<Element STATUS at 0x11708d4d0>
>>> tree[0].text
>>> tree[0].text is None
True

If you set the text attribute of the <STATUS> tag to an empty string, you should get what you're looking for:

>>> tree[0].text = ''
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS></STATUS>.</ERROR>'

With this is mind, you can probably walk a DOM tree and fix up text attributes before writing out your XML. Something like this:

# prevent creation of self-closing tags
for node in tree.iter():
    if node.text is None:
        node.text = ''

If you tostring lxml dom is HTML, you can use

etree.tostring(html_dom, method='html')

to prevent self-closing tag like <a />

Tags:

Python

Lxml