When creating an XML file with Python’s etree, if we write to the file an empty tag using SubElement, I get:
<MyTag />
Unfortunately, our XML parser library used in Fortran doesn’t handle this even though it’s a correct tag. It needs to see:
<MyTag></MyTag>
Is there a way to change the formatting rules or something in etree to make this work?
As of Python 3.4, you can use the
short_empty_elementsargument for both thetostring()function and theElementTRee.write()method:In older Python versions, (2.7 through to 3.3), as a work-around you can use the
htmlmethod to write out the document:Both the
ElementTree.write()method and thetostring()function support themethodkeyword 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
htmloutput 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!