人生苦短之我用Python篇(XML模块)

XML模块

http://baike.baidu.com/link?url=-mBgvMdEDU7F05Pw7h_hBt7A0ctYiPm5a_WvKVLknydnRXKRIyydcVZWRjd_5HE2fMW5Ic45Nb4DiuH1azLjjfs1HBMER-4j38pqFgmj4z4spRud6DrmYimbttjfSB105KHP2pDzaVKmfFvL1JrZ4cpmHqEiFiuSZ_LehN3Hp5GqLdEb6mohzYRyFIZadILH3iDqyvAjCbEXGr4qLMLZEq

可扩展标记语言标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言


xml的格式如下,就是通过<>节点来区别数据结构的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
xmltest.xml
tree = ET.parse("xmltest.xml")#解析xml文件
root = tree.getroot()#获取根节点
node.tag
node.attrib
node.text
node.tag = "years"      #修改tag的值
node.text = str(new_year)  #赋予node元素新的值
node.set("updated","no")  #设置node元素的属性值

root.remove(country)





在python中可以用以下模块操作xml(import xml.etree.ElementTree as ET

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")#解析xml文件
root = tree.getroot()
print(root.tag)

# 遍历xml文档
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag,i.attrib,i.text)

# 只遍历year 节点
for node in root.iter('year'):
    print(node.tag, node.text)


修改和删除xml文档内容

# import xml.etree.ElementTree as ET
#
# tree = ET.parse("xmltest.xml")#解析xml文件
# root = tree.getroot()
# print(root.tag)
#
# # 遍历xml文档
# for child in root:
#     print(child.tag, child.attrib)
#     for i in child:
#         print(i.tag,i.attrib,i.text)
#
# # 只遍历year 节点
# for node in root.iter('year'):
#     print(node.tag, node.text)



import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")
root = tree.getroot()

# 修改
for node in root.iter("year"):
    new_year = int(node.text) + 1
    node.text = str(new_year)#赋予node元素新的值
    node.set("updated","no")#设置node元素的属性值

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")

自己创建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")
name.text = '陈泰成'
age.text = "22"
sex.text = ''

name = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name,"age")
name.text = "哈利路亚"
age.set("is_privary","yes")
age.text = "18"

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

原文地址:https://www.cnblogs.com/tcheng/p/6533711.html