day22_5-xml模块


# 参考资料:
# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园
# https://www.cnblogs.com/guojintao/articles/9070485.html

# ------------------------------------------------------------
# ********************day22_5-xml模块 *******************
# ********************day22_5-xml模块 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
'''
# ------------------------------------------------------------
# # 1、xml模块 的解释
# # # xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,
# # # 不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融
# # # 行业的很多系统的接口还主要是xml。
# # # xml的格式如下,就是通过<>节点来区别数据结构的
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 2、对xml文件读取标签
# # # getroot获取根节点
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 3、对xml文件读取二级的标签、属性
# # # tag、attrib
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 4、对xml文件读取二级的标签、属性、内容
# # # tag、attrib、test
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 5、对xml文件只遍历year 节点
# # # node
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 6、对xml文件进行修改
# # # n
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 7、对xml文件进行删除
# # # findall可以找的范围比较广一点, find可以找的范围比较小一些
# # # 先findall,在find,否则会报错
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 8、生成xml文档对象
# ------------------------------------------------------------

# ------------------------------------------------------------
# # 9、生成xml文档对象并且打印,生成格式
# ------------------------------------------------------------


------------------------------------------------分割线-------------------------------------------------

------------------------------------------------分割线-------------------------------------------------

------------------------------------------------分割线-------------------------------------------------


'''
# ------------------------------------------------------------
# # 1、xml模块 的解释
# # # xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,
# # # 不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融
# # # 行业的很多系统的接口还主要是xml。
# # # xml的格式如下,就是通过<>节点来区别数据结构的
# ------------------------------------------------------------
'''


'''
# 下面实验中所用到的xml文件内容。
# 储存位置与执行文件同目录
# 名称:xml_lesson
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes">2010</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes">2013</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year updated="yes">2013</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

'''


'''
# ------------------------------------------------------------
# # 2、对xml文件读取标签
# # # getroot获取根节点
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
# print(root.tag)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # data
# #
# # Process finished with exit code 0


'''
# ------------------------------------------------------------
# # 2、对xml文件读取一级的标签、属性
# # # tag、attrib
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
# print(root.tag)
#
# for i in root:
#     print("标签:",i.tag)
#     print("属性:",i.attrib)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # data
# # 标签: country
# # 属性: {'name': 'Liechtenstein'}
# # 标签: country
# # 属性: {'name': 'Singapore'}
# # 标签: country
# # 属性: {'name': 'Panama'}
# #
# # Process finished with exit code 0






'''
# ------------------------------------------------------------
# # 3、对xml文件读取二级的标签、属性
# # # tag、attrib
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
# print(root.tag)
#
# for i in root:
#     print( ("一级标签:  "+i.tag).center(60,"-"))
#     print( ("一级属性:  "+str(i.attrib)).center(60,"-"))
#     for j in i:
#         print("二级标签:",j.tag)
#         print("二级属性:",j.attrib)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # data
# # -----------------------一级标签:  country-----------------------
# # --------------一级属性:  {'name': 'Liechtenstein'}--------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级标签: neighbor
# # 二级属性: {'direction': 'E', 'name': 'Austria'}
# # 二级标签: neighbor
# # 二级属性: {'direction': 'W', 'name': 'Switzerland'}
# # -----------------------一级标签:  country-----------------------
# # ----------------一级属性:  {'name': 'Singapore'}----------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级标签: neighbor
# # 二级属性: {'direction': 'N', 'name': 'Malaysia'}
# # -----------------------一级标签:  country-----------------------
# # -----------------一级属性:  {'name': 'Panama'}------------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级标签: neighbor
# # 二级属性: {'direction': 'W', 'name': 'Costa Rica'}
# # 二级标签: neighbor
# # 二级属性: {'direction': 'E', 'name': 'Colombia'}
# #
# # Process finished with exit code 0







'''
# ------------------------------------------------------------
# # 4、对xml文件读取二级的标签、属性、内容
# # # tag、attrib、test
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
# print(root.tag)
#
# for i in root:
#     print( ("一级标签:  "+i.tag).center(60,"-"))
#     print( ("一级属性:  "+str(i.attrib)).center(60,"-"))
#     for j in i:
#         print("二级标签:",j.tag)
#         print("二级属性:",j.attrib)
#         print("二级内容:",j.text)
#     print("\n")
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # data
# # -----------------------一级标签:  country-----------------------
# # --------------一级属性:  {'name': 'Liechtenstein'}--------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 2
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 2010
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级内容: 141100
# # 二级标签: neighbor
# # 二级属性: {'direction': 'E', 'name': 'Austria'}
# # 二级内容: None
# # 二级标签: neighbor
# # 二级属性: {'direction': 'W', 'name': 'Switzerland'}
# # 二级内容: None
# #
# #
# # -----------------------一级标签:  country-----------------------
# # ----------------一级属性:  {'name': 'Singapore'}----------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 5
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 2013
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级内容: 59900
# # 二级标签: neighbor
# # 二级属性: {'direction': 'N', 'name': 'Malaysia'}
# # 二级内容: None
# #
# #
# # -----------------------一级标签:  country-----------------------
# # -----------------一级属性:  {'name': 'Panama'}------------------
# # 二级标签: rank
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 69
# # 二级标签: year
# # 二级属性: {'updated': 'yes'}
# # 二级内容: 2013
# # 二级标签: gdppc
# # 二级属性: {}
# # 二级内容: 13600
# # 二级标签: neighbor
# # 二级属性: {'direction': 'W', 'name': 'Costa Rica'}
# # 二级内容: None
# # 二级标签: neighbor
# # 二级属性: {'direction': 'E', 'name': 'Colombia'}
# # 二级内容: None
# #
# #
# #
# # Process finished with exit code 0







'''
# ------------------------------------------------------------
# # 5、对xml文件只遍历year 节点
# # # node
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
#
# for node in root.iter("year"):
#     print("标签:", node.tag, "内容: ",node.text)
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # 标签: year 内容:  2010
# # 标签: year 内容:  2013
# # 标签: year 内容:  2013
# #
# # Process finished with exit code 0






'''
# ------------------------------------------------------------
# # 6、对xml文件进行修改
# # # n
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
#
# for node in root.iter("year"):
#     new_year = int(node.text) + 1
#     new_year = str(new_year) + "--->+1"
#     node.text = new_year
#     node.set("updated","yes")
# # 写入文件,如果文件名与原文件同名, 原文件将被覆盖
# tree.write("xml_lesson-new")
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# #
# # Process finished with exit code 0







'''
# ------------------------------------------------------------
# # 7、对xml文件进行删除
# # # findall可以找的范围比较广一点, find可以找的范围比较小一些
# # # 先findall,在find,否则会报错
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# tree = ET.parse("xml_lesson")
# root = tree.getroot()
#
# for country in root.findall("country"):
#     rank = int(country.find("rank").text)
#     if rank > 50:
#         root.remove(country)
# # 写入文件,如果文件名与原文件同名, 原文件将被覆盖
# tree.write("xml_lesson-remove")

# D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
#
# Process finished with exit code 0








'''
# ------------------------------------------------------------
# # 8、生成xml文档对象
# ------------------------------------------------------------
'''
# import xml.etree.ElementTree as ET
# new_xml = ET.Element("namelist")
# '''
# 生成根节点,如下:
# <namelist>
#
# </namelist>
# '''
#
# name = ET.SubElement(new_xml,"name--1",attrib={"enrolled": "yes"})
# age  = ET.SubElement(name,   "age---1",attrib={"checked": "no"})
# sex  = ET.SubElement(name,   "sex---1")
# sex.text = "male"
#
# name2 = ET.SubElement(new_xml,"name--2",attrib={"enrolled": "no"})
# age2  = ET.SubElement(name2,   "age---2",attrib={"checked": "yes"})
# # 即使是写入整数,也应该写为字符串模式
# age2.text = '22'
# sex  = ET.SubElement(name,   "sex---1")
#
# # 生成文档对象
# et = ET.ElementTree(new_xml)
# et.write("test.xml", encoding="utf-8", xml_declaration=True)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# #
# # Process finished with exit code 0









'''
# ------------------------------------------------------------
# # 9、生成xml文档对象并且打印,生成格式
# ------------------------------------------------------------
'''
#
# import xml.etree.ElementTree as ET
# new_xml = ET.Element("namelist")
# '''
# 生成根节点,如下:
# <namelist>
#
# </namelist>
# '''
#
# name = ET.SubElement(new_xml,"name--1",attrib={"enrolled": "yes"})
# age  = ET.SubElement(name,   "age---1",attrib={"checked": "no"})
# sex  = ET.SubElement(name,   "sex---1")
# sex.text = "male"
#
# name2 = ET.SubElement(new_xml,"name--2",attrib={"enrolled": "no"})
# age2  = ET.SubElement(name2,   "age---2",attrib={"checked": "yes"})
# # 即使是写入整数,也应该写为字符串模式
# age2.text = '22'
# sex  = ET.SubElement(name,   "sex---1")
#
# # 生成文档对象
# et = ET.ElementTree(new_xml)
# et.write("test.xml", encoding="utf-8", xml_declaration=True)
# # 打印生成的格式
# ET.dump(new_xml)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_5-xml.py
# # <namelist><name--1 enrolled="yes"><age---1 checked="no" /><sex---1>male</sex---1><sex---1 /></name--1><name--2 enrolled="no"><age---2 checked="yes">22</age---2></name--2></namelist>
# #
# # Process finished with exit code 0

  



posted @ 2018-08-07 19:44  JY小脚丫  阅读(1017)  评论(0编辑  收藏  举报