Python之操作XML文件

     本文通过实例来讲解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.tag
print 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.text

tree = 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.text

title =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.text

print "\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——  star
001:attrib is——  {'category': 'COOKING'}
001:text is——  
		
002:child of child_of_root text is——  Everyday Italian
002:child of child_of_root text is——  kuBoy
002:child of child_of_root text is——  20
002:child of child_of_root text is——  11011
001:tag is——  star
001:attrib is——  {'category': 'CHILDREN'}
001:text is——  
		
002:child of child_of_root text is——  Harry Potter
002:child of child_of_root text is——  plGirl
002:child of child_of_root text is——  18
002:child of child_of_root text is——  123321
001:tag is——  star
001:attrib is——  {'category': 'WEB'}
001:text is——  
		
002:child of child_of_root text is——  Learning XML
002:child of child_of_root text is——  goodBoy
002:child of child_of_root text is——  21
002:child of child_of_root text is——  1234509876

003: start
003:text is——  Everyday Italian
003:text is——  kuBoy
003:text is——  20
003:text is——  11011
004: text is—— Everyday Italian
005: text is—— 11011
006: go
006:text is——  Everyday Italian
007: text is—— 11011
006:text is——  Harry Potter
007: text is—— 123321
006:text is——  Learning XML
007: text is—— 1234509876


008: tag is——   stars
009: attrib is——  {}
010: text is——  
	


008: tag is——   star
009: attrib is——  {'category': 'COOKING'}
010: text is——  
		


008: tag is——   name
009: attrib is——  {'lang': 'en'}
010: text is——  Everyday Italian


008: tag is——   sex
009: attrib is——  {}
010: text is——  kuBoy


008: tag is——   age
009: attrib is——  {}
010: text is——  20


008: tag is——   qq
009: attrib is——  {}
010: text is——  11011


008: tag is——   star
009: attrib is——  {'category': 'CHILDREN'}
010: text is——  
		


008: tag is——   name
009: attrib is——  {'lang': 'en'}
010: text is——  Harry Potter


008: tag is——   sex
009: attrib is——  {}
010: text is——  plGirl


008: tag is——   age
009: attrib is——  {}
010: text is——  18


008: tag is——   qq
009: attrib is——  {}
010: text is——  123321


008: tag is——   star
009: attrib is——  {'category': 'WEB'}
010: text is——  
		


008: tag is——   name
009: attrib is——  {'lang': 'en'}
010: text is——  Learning XML


008: tag is——   sex
009: attrib is——  {}
010: text is——  goodBoy


008: tag is——   age
009: attrib is——  {}
010: text is——  21


008: tag is——   qq
009: attrib is——  {}
010: text is——  1234509876


011: tag is—— name
011: attrib is—— {'lang': 'en'}
011: text is—— Everyday Italian
011: tag is—— name
011: attrib is—— {'lang': 'en'}
011: text is—— Harry Potter
011: tag is—— name
011: attrib is—— {'lang': 'en'}
011: text is—— Learning XML


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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值