【1.71】xml 再学习

 

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

 

# 查看操作 test.xml 文件   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>
test.xml
# 查看操作 test.xml 文件   xml 就是标签语言

import xml.etree.ElementTree as  xet  # 可以用 as 简化导入的模块

tree = xet.parse("test.xml")  # 通过xml 的模块方法 parse (分析)  得到树结构
root = tree.getroot()  # 通过对获取的 tree  再获取 getroot 树根
print(root)  # 获取的树根 的 对象的地址  所有操作都是基于 root 来操作的
print(root.tag)  # 获取的树根标签 为 data



# 获取树根下的 二级根 对象
print("查看二级根对象的地址".center(50,"*"))
for son in root:
    print(son)  # 打印出来全是二级根 对象的地址

# <Element 'country' at 0x000000000052C818>
# <Element 'country' at 0x00000000028B57C8>
# <Element 'country' at 0x00000000028B5958>




# xml 的操作 增 删 改 查 都是基于  root 来操作的
print("查看二级根对象下的三级树对象的地址值".center(50,"*"))
for son in root:
    for gson in son:
        print(gson)

# <Element 'rank' at 0x00000000023577C8>
# <Element 'year' at 0x00000000029070E8>
# <Element 'gdppc' at 0x0000000002907138>
# <Element 'neighbor' at 0x0000000002907778>
# <Element 'neighbor' at 0x0000000002924778>
# <Element 'rank' at 0x0000000002924818>
# <Element 'year' at 0x0000000002924868>
# <Element 'gdppc' at 0x00000000029248B8>
# <Element 'neighbor' at 0x0000000002924908>
# <Element 'rank' at 0x00000000029249A8>
# <Element 'year' at 0x00000000029249F8>
# <Element 'gdppc' at 0x0000000002924A48>
# <Element 'neighbor' at 0x0000000002924A98>
# <Element 'neighbor' at 0x0000000002924AE8>


print ("查看二级对象的值".center(50,"*"))
for son in root:
    print(son.tag)
# country
# country
# country


print ("查看二级对象的所有属性的值 attrib".center(50,"*"))
for son in root:
    print(son.attrib)
# {'name': 'Liechtenstein'}
# {'name': 'Singapore'}
# {'name': 'Panama'}



print ("查看二级对象的某个属性的值 attrib name ".center(50,"*"))
for son in root:
    print(son.attrib["name"])
# Liechtenstein
# Singapore
# Panama

print ("查看二级对象的某个属性的值 text".center(50,"*"))
for son in root:
    print(son.text)
# Liechtenstein
# Singapore
# Panama
xml son 查看
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

print ("查看三级对象的地址值 ".center(50,"*"))
for son in root:
    for gson in  son:
        print (gson)
print ("查看三级对象的标签值 ".center(50,"*"))
for son in root:
    for gson in  son:
        print (gson.tag)
print ("查看三级对象的属性值 attrib".center(50,"*"))
for son in root:
    for gson in  son:
        print (gson.attrib)
print ("查看三级对象的属性值 text".center(50,"*"))
for son in root:
    for gson in  son:
        print (gson.text)
xml gson 查看
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

# 查看某个节点的相关值(包含son 节点 以及gson节点)
print("查看某个(包含son、gson)节点对象的地址".center(50,"*"))
for node in root.iter("country"):
    print (node)
for node in root.iter("year"):
    print(node)
for node in root.iter("rank"):
    print(node)


print("查看某个(包含son、gson)节点对象的值 tag".center(50,"*"))
for node in root.iter("year"):
    print (node.tag)
#打印的全是 year
for node in root.iter("country"):
    print (node.tag)
#打印的全是 country



print("查看某个(包含son、gson)节点对象的值 text或attrib".center(50,"*"))
for node in root.iter("year"):
    print (node.attrib)
#打印的全是 {} 因为没有字典  就只有文本
for node in root.iter("year"):
    print (node.text)


#下面两个是一样的 但是第一个就会找出所有 country 的标签
#第二个只是找出son 节点的 country 的标签
for node in root.iter("country"):
    print (node.attrib)
#打印的全是 就是字典  和  下面的二级节点一样
print("只是找出son 节点的 country 的标签 字典值")
for son in root:
    print(son.attrib)

print("所有节点的 country 标签 的 text值,但是为空")
for node in root.iter("country"):
    print (node.text)
print("所有节点的 country 标签 的 text值,但是为空")
查找所有二级三级节点的值
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

#修改
for node in root.iter("rank"):
    print(node.text)
for node in root.iter("rank"):
    new_rank = int(node.text)+1
    node.text = str(new_rank)
    node.set("updated","yes")
    node.set("version","1.0")
tree.write("test.xml")
xml 的修改
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

#删除
for country  in root.findall("country"):
    print(country)
    year = int(country.find("year").text)
    if year <  2013:
        root.remove(country)
tree.write("output.xml")
删除son节点(还没有搞清楚是否能删除节点中某个属性)
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

#增加  
for country in root.findall("country"):
    for year in country.findall("year"):
        if int(year.text) > 2012:
            year2 = xet.Element("year2")
            year2.text = "新年"
            year2.attrib={"update":"yes"}
            year2.set("version","1.0")
            country.append(year2)  #往 country 节点下添加子节点
tree.write("a.xml.swap")
给son节点下添加一个son节点
import  xml.etree.cElementTree as xet
tree = xet.parse("test.xml")
root = tree.getroot()

#增加 son 节点
for country in root.findall('country'):
    for year in country.findall('year'):
        if int(year.text) > 2010:
            month = xet.Element('month')
            month.text = '30days'
            month.attrib = {'name': 'month'}
            country.append(month)
tree.write('b.xml')
给son节点下增加一个son节点

 

posted @ 2016-04-22 15:44  科学小怪癖  阅读(90)  评论(0)    收藏  举报