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的格式,以及代码中的参数对应的内容。具体如下: