As of Python 3.4, you can use the short_empty_elements
argument for both the tostring()
function and the ElementTRee.write()
method:
>>> from xml.etree import ElementTree as ET
>>> ET.tostring(ET.fromstring('<mytag/>'), short_empty_elements=False)
b'<mytag></mytag>'
In older Python versions, (2.7 through to 3.3), as a work-around you can use the html
method to write out the document:
>>> from xml.etree import ElementTree as ET
>>> ET.tostring(ET.fromstring('<mytag/>'), method='html')
'<mytag></mytag>'
Both the ElementTree.write()
method and the tostring()
function support the method
keyword argument.
On even earlier versions of Python (2.6 and before) you can install the external ElementTree library; version 1.3 supports that keyword.
Yes, it sounds a little weird, but the html
output mostly outputs empty elements as a start and end tag. Some elements still end up as empty tag elements; specifically <link/>
, <input/>
, <br/>
and such. Still, it's that or upgrade your Fortran XML parser to actually parse standards-compliant XML!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…