Python 创建XML 的三种方式

from xml.etree import ElementTree as ET

# 方式一,通过Element这个类来创建element
# 创建element
ele = ET.Element("Family", {"age": "40"})
# 创建ElementTree
tree = ET.ElementTree(ele)

# 方式二,通过makeelement 创建element
son = ele.makeelement("aaa", {"sex": "man"})
ele.append(son)

# 方式三,通过SubElement 来创建 下级element
grandson = ET.SubElement(son, "grandson", {"age": "12"})
grandson.text = "this is 孙子"
tree.write("235.xml", encoding='utf-8',xml_declaration=True,short_empty_elements=False)

 

上一篇:R语言MCMC:Metropolis-Hastings采样用于回归的贝叶斯估计


下一篇:CategoricalNF