day05_03 xml创建

xml创建.py

__author__ = "Alex Li"

import xml.etree.ElementTree as ET

new_xml = ET.Element("personinfolist")
personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "yes"})
name = ET.SubElement(personinfo, "name")
name.text = "Alex Li"
age = ET.SubElement(personinfo, "age", attrib={"checked": "no"})
sex = ET.SubElement(personinfo, "sex")
age.text = '56'
personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "no"})
name = ET.SubElement(personinfo2, "name")
name.text = "Oldboy Ran"
age = ET.SubElement(personinfo2, "age")
age.text = '19'

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式

test.xml

<?xml version='1.0' encoding='utf-8'?>
<personinfolist>
    <personinfo enrolled="yes">
        <name>Alex Li</name>
        <age checked="no">56</age>
        <sex />
    </personinfo>
    <personinfo enrolled="no">
        <name>Oldboy Ran</name>
        <age>19</age>
    </personinfo>
</personinfolist>
上一篇:英文论文里的缩写:e.g. etc. et al. i.e.


下一篇:python+appium自动化测试-元素定位之android uiautomatorandroid