Do Not Collapse Empty Nodes In XML Output
I'm using python's xml.etree.ElementTree to represent an XML document. I want to output it to text but I want to keep empty elements (elements with no children) expanded, instead o
Solution 1:
You can pass method="html"
to the tostring()
call.
Demo:
>>> import xml.etree.ElementTree as etree
>>> data = """
... <root>
... <person/>
... <person></person>
... </root>
... """
>>> tree = etree.fromstring(data)
>>> print etree.tostring(tree, method="html")
<root>
<person></person>
<person></person>
</root>
Solution 2:
Just use: ElementTree.tostring(element, short_empty_elements=False)
Post a Comment for "Do Not Collapse Empty Nodes In XML Output"