I have an xml.dom.Element object and would like to convert it to a string containing XML. There doesn't seem to be a method for this or am I missing something?
1 Answer
Use the toprettyxml or toxml method:
elt.toprettyxml(indent = ' ')
For example,
import xml.dom.minidom as minidom
doc = minidom.Document()
foo = doc.createElement("foo")
doc.appendChild(foo)
print(foo.__class__)
# xml.dom.minidom.Element
print(foo.toprettyxml(indent = ' '))
# <foo/>