漂亮打印Python xml.dom.Element对象
我有一个xml.dom.Element对象,想把它转换成一个包含XML内容的字符串。好像没有直接的方法可以做到这一点,还是我漏掉了什么?
1 个回答
4
使用 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/>