Python之操作XML文件
来源:互联网 发布:熟悉掌握办公软件 编辑:程序博客网 时间:2024/05/18 03:57
本文通过实例来讲解Python代码对XML文件的操作,包括解析XML、读取XML
首先,我们先创建一个XML文件,内容如下
<stars><star category="COOKING"><name lang="en">Everyday Italian</name><sex>帅哥</sex><age>20</age><qq>11011</qq></star><star category="CHILDREN"><name lang="en">Harry Potter</name><sex>妹纸</sex><age>18</age><qq>123321</qq></star><star category="WEB"><name lang="en">Learning XML</name><sex>妹纸</sex><age>21</age><qq>1234509876</qq></star></stars>
然后,我们看下具体的代码实现
# -*- 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类的实例: <Element 'stars' at 0x27a2128>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的格式,以及代码中的参数对应的内容。具体如下:
阅读全文
0 0
- Python之XML文件操作
- Python之操作XML文件
- [python]python操作xml文件
- python操作XML文件------elementtree
- Python xml.dom.minidom 操作XML文件
- python之xml操作问题
- python之文件操作
- python之文件操作
- python 之文件操作
- python 之文件操作
- python之文件操作
- Python之文件操作
- python之文件操作
- python之文件操作
- python之文件操作
- python之文件操作
- Python之文件操作
- Python之文件操作
- Android 扩大ImageView的点击区域,解决按钮有效区域小的问题
- kafka->spark->streaming->mysql(scala)实时数据处理示例
- JAVA程序员必看的15本书
- jdk1.8 HashMap源码分析(put函数)
- MapReduce编程模型及优化技巧
- Python之操作XML文件
- java8 自定义TemporalAdjuster
- Window下 Qt 编译MySQL驱动
- 链表各类操作详解(讲得非常详细)
- ALTER TABLE ADD 增加多个字段 外键约束
- 数据库事务
- jdk1.8 HashMap源码分析(构造函数)
- js设计模式之构造函数
- POJ