我有一个 xml.dom.Element 对象,想将其转换为包含 XML 的字符串。似乎没有办法解决这个问题,还是我遗漏了一些东西?
最佳答案
使用toprettyxml或toxml方法:
elt.toprettyxml(indent = ' ')
例如,
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/>
关于python - pretty-print Python xml.dom.Element 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087744/