day05_03 xml创建

Posted flytoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

以上是关于day05_03 xml创建的主要内容,如果未能解决你的问题,请参考以下文章

day03_05 Python程序文件执行和与其他编程语言对比

片段xml错误?

day37 03-Hibernate二级缓存:集合缓冲区特点

day12_03 orm创建mysql表结构

day05_04 数据类型-数值布尔值字符串简介

python学习day05