Python etree control empty tag format Python etree control empty tag format xml xml

Python etree control empty tag format


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!


This was directly solved in Python 3.4. From then, the write method of xml.etree.ElementTree.ElementTree has the short_empty_elements parameter which:

controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

More details in the xml.etree documentation.


If you have sed available, you could pipe the output of your python script to

sed -e "s/<\([^>]*\) \/>/<\1><\/\1>/g"

Which will find any occurence of <Tag /> and replace it by <Tag></Tag>