好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python序列化功能之xml的详细介绍

这篇文章主要为大家详细介绍了python序列化功能之xml具有一定的参考价值,感兴趣的小伙伴们可以参考一下

<diva_list>
    <diva name="hamasaki ayumi">
        <state flop="yes">1</state>
        <year>1978</year>
        <album_sales_volume>27804358</album_sales_volume>
        <album name="A song for xx" sn="1st"/>
        <album name="LOVEppears" sn="2nd"/>
    </diva>
    <diva name="koda kumi">
        <state flop="yes">2</state>
        <year>1982</year>
     <album_sales_volume>8273371</album_sales_volume>
        <album name="affection" sn="1st"/>
        <album name="grow into one" sn="2nd"/>
    </diva>
</diva_list> 

node.text = str(new_year) #修改内容

node.set("flop","no") #修改标签属性。

tree.write("xmltest.xml")

删除:

for country in root.findall('country'):

rank = int(country.find('rank').text)

if rank > 50:

root.remove(country)

tree.write('output.xml')

root.findall() 用于从根节点开始查找,查找到指定名字的子节点。

root.remove()用于删除一个节点。

生成xml文本。

import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")

name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})

age = ET.SubElement(name,"age",attrib={"checked":"no"})

sex = ET.SubElement(name,"sex")

sex.text = '33'

name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})

age = ET.SubElement(name2,"age")

age.text = '19'

et = ET.ElementTree(new_xml) #生成文档对象

et.write("test.xml", encoding="utf-8",xml_declaration=True)

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

以上就是python序列化功能之xml的详细介绍的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于python序列化功能之xml的详细介绍的详细内容...

  阅读:36次