标签:rom top element title encoding end rand XML dom
from xml.etree import ElementTree as ET from xml.dom import minidom def prettify(elem): """将节点转换成字符串,并添加缩进。 """ rough_string = ET.tostring(elem, ‘utf-8‘) reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t") # 创建根节点 root = ET.Element("famliy") # 创建节点1 # son1 = ET.Element(‘son‘, {‘name‘: ‘儿1‘}) son1 = root.makeelement(‘son‘, {‘name‘: ‘儿1‘}) # 创建节点2 # son2 = ET.Element(‘son‘, {"name": ‘儿2‘}) son2 = root.makeelement(‘son‘, {"name": ‘儿2‘}) # 创建节点1/2的子节点 # grandson1 = ET.Element(‘grandson‘, {‘name‘: ‘儿11‘}) grandson1 = son1.makeelement(‘grandson‘, {‘name‘: ‘儿11‘}) # grandson2 = ET.Element(‘grandson‘, {‘name‘: ‘儿12‘}) grandson2 = son1.makeelement(‘grandson‘, {‘name‘: ‘儿12‘})
#添加 son1.append(grandson1) son1.append(grandson2) # 添加到根节点中 root.append(son1) root.append(son1) raw_str = prettify(root) f = open("xxxoo.xml",‘w‘,encoding=‘utf-8‘) f.write(raw_str) f.close()
标签:rom top element title encoding end rand XML dom
原文地址:http://www.cnblogs.com/mycgy/p/6294490.html