编程语言
首页 > 编程语言> > Python 创建XML 的三种方式

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)

 

标签:XML,son,ElementTree,Python,创建,element,三种,ET,ele
来源: https://www.cnblogs.com/xuwenwei/p/14406936.html