python xml ElementTree浅迹

python xml ElementTree浅迹

xml文件解析

在这里插入图片描述


 *****#红色为标签(tag),橙色为属性名、绿色为属性值(attrib),白色为嵌入数据(text)****

<bookstore>             #根
	<aa>
		<book category="COOKING">        #子
		  <title lang="en">Everyday Italian</title>           #孙子
		  <author>Giada De Laurentiis</author> 
		  <year>2005</year> 
		  <price>30.00</price> 
		</book>   
	</aa>
	<book category="CHILDREN">
		<bb>
	  		<title lang="en">Harry Potter</title> 
	  		<author>J K. Rowling</author> 
	  		<year>2005</year> 
	  		<price>29.99</price> 
	  	</bb>
	</book>
	<book category="WEB">
	  <title lang="en">Learning XML</title> 
	  <author>Erik T. Ray</author> 
	  <year>2003</year> 
	  <price>39.95</price> 
	</book>
	<book></book>
</bookstore>
	

网上dang的xml树结构

部分函数释义

1.用ElementTree处理xml文件首先要导包

		import xml.etree.ElememtTree as ET
tree = ET.parse(xmlFile)  #将xmlFile文件构造成一个ElementTree对象
root = tree.getroot()   # 返回ElementTree的根节点 <bookstore>  
for book in root.findall('book'):  #只在子节点中查找不会往下(孙子节点)查找
	name1 = book.find('title'# 输出:None <Element title at 308c968>
	                            #只在子节点查找tag为title的第一个element
	if book.find('title') != None:
        name2 = book.find('title').text #  输出:Learning XML
for book in root.iter('book'):    #在所有的子树中(包括子节点以下所有节点)查找
	name1 = book.find('title'#输出<Element book at 309c788> <Element book at 309c850> <Element book at 309c940>
	name = book.get('category') # COOKING  CHILDREN  WEB
	                             #只在当前层(<book category="COOKING">)查找属性
	book.set('time', '000')   #在book层修改或添加属性
	if book:
		print 1                 #若book有子节点(tag)为True
	print len(book)             #book 的子节点(tag)的个数
	                            #输出4 1 4 0
bookstore = ET.Element('bookstore')
......
tree = ET.ElementTree(bookstore)
tree.write('filename')

find、findall只在子节点中查找不会往下(孙子节点)查找
iter、getiterator(python2.6以下使用)在所有的子树中(包括子节点以下所有节点)查找
get、set只在当前标签(tag下查找、修改、更新)

猜你喜欢

转载自blog.csdn.net/u014686859/article/details/86629831
今日推荐