博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python之操作XML文件
阅读量:4049 次
发布时间:2019-05-25

本文共 4318 字,大约阅读时间需要 14 分钟。

     本文通过实例来讲解Python代码对XML文件的操作,包括解析XML、读取XML

首先,我们先创建一个XML文件,内容如下

Everyday Italian
帅哥
20
11011
Harry Potter
妹纸
18
123321
Learning XML
妹纸
21
1234509876
然后,我们看下具体的代码实现

# -*- coding: utf-8 -*# 导入模块from xml.etree import ElementTree#从硬盘的xml文件读取数据,tree为ElementTree实例#tree = ElementTree.parse('t.xml')tree = ElementTree.ElementTree(file='a.xml')#获取根节点元素,root 为Element类的实例root = tree.getroot()print  "Element类的实例: ",root# 获取根元素的名称和属性print root.tagprint root.attrib#  迭代根元素对象,获取各子元素的名称,属性文本for child_of_root in root:    print '001:tag is—— ', child_of_root.tag  #输出为:  star    print '001:attrib is—— ',child_of_root.attrib    print '001:text is—— ',child_of_root.text    for child_of_child in child_of_root:        print '002:child of child_of_root text is—— ',child_of_child.texttree = ElementTree.ElementTree(file='a.xml')# 返回查找到的第一个元素star = tree.find('star')print "\n003: start"for sub in star:    print "003:text is—— ",sub.text#根据第一个元素查看其子元素title =star.find('name')print "004: text is——",title.texttitle =star.find('qq')print "005: text is——",title.text# 返回所有star元素stars = tree.findall('star')print "006: go"for star in stars:    title = star.find('name')    print "006:text is—— ", title.text    price = star.find('qq')    print "007: text is——", price.textprint "\n"# 调用对象的iter() 方法表里所有元素for elem in tree.iter():    print "008: tag is——  ", elem.tag    print "009: attrib is—— ", elem.attrib    print "010: text is—— ", elem.text    print "\n"for elem in tree.iter(tag='name'):    print "011: tag is——", elem.tag    print "011: attrib is——", elem.attrib    print "011: text is——", elem.text

最后,我们看一下运行结果:

Element类的实例:  
stars{}001:tag is—— star001:attrib is—— {'category': 'COOKING'}001:text is—— 002:child of child_of_root text is—— Everyday Italian002:child of child_of_root text is—— kuBoy002:child of child_of_root text is—— 20002:child of child_of_root text is—— 11011001:tag is—— star001:attrib is—— {'category': 'CHILDREN'}001:text is—— 002:child of child_of_root text is—— Harry Potter002:child of child_of_root text is—— plGirl002:child of child_of_root text is—— 18002:child of child_of_root text is—— 123321001:tag is—— star001:attrib is—— {'category': 'WEB'}001:text is—— 002:child of child_of_root text is—— Learning XML002:child of child_of_root text is—— goodBoy002:child of child_of_root text is—— 21002:child of child_of_root text is—— 1234509876003: start003:text is—— Everyday Italian003:text is—— kuBoy003:text is—— 20003:text is—— 11011004: text is—— Everyday Italian005: text is—— 11011006: go006:text is—— Everyday Italian007: text is—— 11011006:text is—— Harry Potter007: text is—— 123321006:text is—— Learning XML007: text is—— 1234509876008: tag is—— stars009: attrib is—— {}010: text is—— 008: tag is—— star009: attrib is—— {'category': 'COOKING'}010: text is—— 008: tag is—— name009: attrib is—— {'lang': 'en'}010: text is—— Everyday Italian008: tag is—— sex009: attrib is—— {}010: text is—— kuBoy008: tag is—— age009: attrib is—— {}010: text is—— 20008: tag is—— qq009: attrib is—— {}010: text is—— 11011008: tag is—— star009: attrib is—— {'category': 'CHILDREN'}010: text is—— 008: tag is—— name009: attrib is—— {'lang': 'en'}010: text is—— Harry Potter008: tag is—— sex009: attrib is—— {}010: text is—— plGirl008: tag is—— age009: attrib is—— {}010: text is—— 18008: tag is—— qq009: attrib is—— {}010: text is—— 123321008: tag is—— star009: attrib is—— {'category': 'WEB'}010: text is—— 008: tag is—— name009: attrib is—— {'lang': 'en'}010: text is—— Learning XML008: tag is—— sex009: attrib is—— {}010: text is—— goodBoy008: tag is—— age009: attrib is—— {}010: text is—— 21008: tag is—— qq009: attrib is—— {}010: text is—— 1234509876011: tag is—— name011: attrib is—— {'lang': 'en'}011: text is—— Everyday Italian011: tag is—— name011: attrib is—— {'lang': 'en'}011: text is—— Harry Potter011: tag is—— name011: attrib is—— {'lang': 'en'}011: text is—— Learning XML

所以, 解析XML之前,需要了解XML的格式,以及代码中的参数对应的内容。具体如下:

你可能感兴趣的文章
Oracle中定时执行问题
查看>>
三时业
查看>>
佛教三宝-三皈依
查看>>
杂阿含经喻世间有四等马
查看>>
考研前夜涂笔
查看>>
英语复试自我介绍
查看>>
什么是熵?
查看>>
拼凑、摘抄-评李代平的软件工程第二版
查看>>
误传了数千年的几个名句
查看>>
韩复榘经典语录
查看>>
厅、部、局、司区分大小
查看>>
VS2005中使用C#编写MDI窗口根据子窗口个数控制菜单项的enabled属性
查看>>
北川邓家“刘汉小学”无一死亡奇迹背后的真相
查看>>
救灾,从来没有胜利
查看>>
.net 2.0中ConfigurationManager替代了原来的ConfigurationSettings
查看>>
Asp.net 2.0中使用Datawindow.net2.0
查看>>
常用命名法:骆驼命名法,匈牙利命名法和帕斯卡命名法
查看>>
Server.MapPath方法测试结果
查看>>
Asp.net 默认配置下,Session莫名丢失的原因及解决办法
查看>>
Datawindow.net中如何使用Calendar控件
查看>>