官方文档 : https://wiki.python.org/moin/MiniDom
注意事项:在解析或生成Dom结构的过程中,其对象的内部编码是Unicode类型的,当要写入文件时,需要先编码为 utf-8
示例代码,filmlist是一个字典列表,本函数的结果可作为Solr的入库XML
def gen_xml(filmlist):
root = ET.Element("add")
for film in filmlist:
doc = ET.SubElement(root, "doc")
for key in film.iterkeys():
field = ET.SubElement(doc, "field")
field.set("name", key)
field.text = film[key]
tree = ET.ElementTree(root)
f=open("test.xml",'w')
with open('test.xml', 'w') as f:
f.write(minidom.parseString(ET.tostring(tree.getroot(), 'utf-8')).toprettyxml(indent=" ").encode('utf-8'))