代码
import requests
from xml.etree import ElementTree as ET
#创建根节点
new_xml = ET.Element("namelist")
#创建子节点
name1 = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age1 = ET.SubElement(name1,"age",attrib={"checked":"no"})
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age2 = ET.SubElement(name2,"age")
#生成文档对象
et = ET.ElementTree(new_xml)
et.write("test1.xml",encoding="utf-8",xml_declaration=True)
test1.xml
<?xml version='1.0' encoding='utf-8'?>
<namelist>
<name enrolled="yes">
<age checked="no" />
</name>
<name enrolled="no">
<age />
</name>
</namelist>