I have a (old) tool that does not understand self-closing tags, for example <STATUS/>. So, we need to serialize our XML files open / close tags as follows: <STATUS></STATUS>.
I currently have:
>>> from lxml import etree
>>> para = """<ERROR>The status is <STATUS></STATUS>.</ERROR>"""
>>> tree = etree.XML(para)
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS/>.</ERROR>'
How can I serialize with open / closed tags?
<ERROR>The status is <STATUS></STATUS>.</ERROR>
Decision
Courtesy of wildwilhelm , below :
>>> from lxml import etree
>>> para = """<ERROR>The status is <STATUS></STATUS>.</ERROR>"""
>>> tree = etree.XML(para)
>>> for status_elem in tree.xpath("//STATUS[string() = '']"):
... status_elem.text = ""
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS></STATUS>.</ERROR>'
source
share