我正在尝试在 python 中生成 XML 文件,但它没有缩进输出是直线。
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
name = str(request.POST.get('name'))
top = Element('scenario')
environment = SubElement(top, 'environment')
cluster = SubElement(top, 'cluster')
cluster.text=name
我尝试使用漂亮的解析器,但它给了我一个错误:'Element' object has no attribute 'read'
import xml.dom.minidom
xml_p = xml.dom.minidom.parse(top)
pretty_xml = xml_p.toprettyxml()
提供给解析器的输入格式是否正确?如果这是错误的方法,请建议另一种缩进方式。