<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的详细介绍的详细内容...