一,XML文件格式介绍
<tag attrib = > text </tag> tail 例:<APP_KEY channel = 'CSDN'> hello123456789 </APP_KEY> - tag,即标签,用于标识该元素表示哪种数据,即APP_KEY - attrib,即属性,用Dictionary形式保存,即{‘channel’ = ‘CSDN’} - text,文本字符串,可以用来存储一些数据,即hello123456789 - tail,尾字符串,并不是必须的。
二,示例
''' 在Python标准库中,ElementTree有两种实现方式:一种是纯Python的实现xml.etree.ElementTree,另一种是速度更快一点的xml.etree.cElementTree。如果不确定环境中是否有cElementTree,可以使用如下的方式导入 但从Python 3.3开始,会默认使用cElementTree来加快速度,但是之前的版本最好使用如上的代码,以提高代码的兼容性。 ''' try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET def makeXML(): # 创建一个根节点,标签名为Root root_element = ET.Element("Root") # 插入两个子节点 sub_element1 = ET.SubElement(root_element, "sub_elemtnt1") sub_element2 = ET.SubElement(root_element, "sub_elemtnt2") for i in range(5): # 设置tag 以及 subb = ET.SubElement(sub_element1, f"name_{i}", tag=f"tag_{i}", attrib={ "name": f"value_{i}"}) # 为标签赋值 subb.text = f"abc{i}" subb2 = ET.SubElement(sub_element2, f"name_{i}", tag=f"tag_{i}", attrib={ "name": f"value_{i}"}) subb2.text = f"efg{i}" # 创建xml树,并将根节点放入其中 xml_tree = ET.ElementTree(root_element) # 将xml 写入到文件中,参数1:xml文件生成的位置和名字,参数2:指定xml编码,参数3:xml 宣言,
# 即:是否有 <?xml version='1.0' encoding='utf-8'?> (这个方法生成的xml文件使用文本打开格式是没有缩进的) xml_tree.write(r"C:Usersv-yunhguDesktopexample.xml", encoding="utf-8", xml_declaration=True) # 第二种写入文件的方法 (这个方法生成的xml文件使用文本打开格式是有缩进的) # ET模块转换root为bytes输出 xml_string = ET.tostring(root_element) from xml.dom import minidom dom = minidom.parseString(xml_string) with open(r"C:Usersv-yunhguDesktopexample2.xml", 'w', encoding='utf-8') as f: # indent为根节点缩进,newl每行数据句末符号,addindent为其他节点缩进 dom.writexml(f, indent=' ', newl=' ', addindent=' ', encoding='utf-8') def getXML(): xmlPath = r"C:Usersv-yunhguDesktopexample2.xml" # 解析xml 文件 xml_tree = ET.parse(xmlPath) # 获取根节点 root_element = xml_tree.getroot() print("根节点名字:", root_element.tag) # 遍历根节点下的子节点 for sub in root_element: print("根节点下的字节点:", sub.tag) for s in sub: print(s.tag, s.attrib, s.text) if __name__ == "__main__": makeXML() getXML()
结果:
example.xml
<?xml version='1.0' encoding='utf-8'?> <Root><sub_elemtnt1><name_0 name="value_0" tag="tag_0">abc0</name_0><name_1 name="value_1" tag="tag_1">abc1</name_1><name_2 name="value_2" tag="tag_2">abc2</name_2><name_3 name="value_3" tag="tag_3">abc3</name_3><name_4 name="value_4" tag="tag_4">abc4</name_4></sub_elemtnt1><sub_elemtnt2><name_0 name="value_0" tag="tag_0">efg0</name_0><name_1 name="value_1" tag="tag_1">efg1</name_1><name_2 name="value_2" tag="tag_2">efg2</name_2><name_3 name="value_3" tag="tag_3">efg3</name_3><name_4 name="value_4" tag="tag_4">efg4</name_4></sub_elemtnt2></Root>
example2.xml
<?xml version="1.0" encoding="utf-8"?> <Root> <sub_elemtnt1> <name_0 name="value_0" tag="tag_0">abc0</name_0> <name_1 name="value_1" tag="tag_1">abc1</name_1> <name_2 name="value_2" tag="tag_2">abc2</name_2> <name_3 name="value_3" tag="tag_3">abc3</name_3> <name_4 name="value_4" tag="tag_4">abc4</name_4> </sub_elemtnt1> <sub_elemtnt2> <name_0 name="value_0" tag="tag_0">efg0</name_0> <name_1 name="value_1" tag="tag_1">efg1</name_1> <name_2 name="value_2" tag="tag_2">efg2</name_2> <name_3 name="value_3" tag="tag_3">efg3</name_3> <name_4 name="value_4" tag="tag_4">efg4</name_4> </sub_elemtnt2> </Root>
根节点名字: Root 根节点下的字节点: sub_elemtnt1 name_0 {'name': 'value_0', 'tag': 'tag_0'} abc0 name_1 {'name': 'value_1', 'tag': 'tag_1'} abc1 name_2 {'name': 'value_2', 'tag': 'tag_2'} abc2 name_3 {'name': 'value_3', 'tag': 'tag_3'} abc3 name_4 {'name': 'value_4', 'tag': 'tag_4'} abc4 根节点下的字节点: sub_elemtnt2 name_0 {'name': 'value_0', 'tag': 'tag_0'} efg0 name_1 {'name': 'value_1', 'tag': 'tag_1'} efg1 name_2 {'name': 'value_2', 'tag': 'tag_2'} efg2 name_3 {'name': 'value_3', 'tag': 'tag_3'} efg3 name_4 {'name': 'value_4', 'tag': 'tag_4'} efg4