---------------------------------------------------------------貌似很簡單,直接試用一下就知道了。。。
from xml.etree import ElementTree
a= '''<?xml version="1.0"?>
<root item_key1='ikey1' item_key2='iKey2' attriball='all' >
root text
<subEle1 subAttrib='subAttrib1' /> sub1's tails
<subEle2 subItem1="ele21" subItem2="ele22">
subEle2 text
<sub2sub1>sub2'sub1 text</sub2sub1>
<sub2sub2>sub2'sub2 text</sub2sub2>
<sub2sub3>
<sub231>just 4 fun</sub231>
</sub2sub3>
<tag1>tag1's text1</tag1>
<tag1>tag1's text2</tag1>
<tag1>tag1's text3</tag1>
<tag1>tag1's text4</tag1>
</subEle2>
<subEle3>subEle3's text</subEle3>
</root>'''
r = ElementTree.fromstring(a) # 從字符串中生成xml樹
>>> ElementTree.tostring(r) # 顯示
>>> ElementTree.dump(r)
>>> ElementTree.dump(r.find('subEle2'))
>>> dir(r) # o是2.5版本
['__delitem__', '__delslice__', '__doc__', '__getitem__', '__getslice__', '__init__', '__len__', '__
module__', '__repr__', '__setitem__', '__setslice__', '_children', 'append', 'attrib', 'clear', 'fin
d', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'keys', 'makeelem
ent', 'remove', 'set', 'tag', 'tail', 'text']
------------------------- root本身的屬性等
>>> str.strip(r.text) # tag、text、items、attrib的所指
'root text'
>>> r.tag
'root'
>>> r.items() # 是個2維元組的列表
[('attriball', 'all'), ('item_key2', 'iKey2'), ('item_key1', 'ikey1')]
>>> r.attrib # 是個集合
{'attriball': 'all', 'item_key2': 'iKey2', 'item_key1': 'ikey1'}
>>> r.get('attriball') # 如果沒有該屬性返回None哈。
'all'
>>> r.keys()
['attriball', 'item_key2', 'item_key1']
>>> if r.attrib.has_key("item_key2"):
... print r.attrib['item_key2']
...
iKey2
------------------------------------- root的孩子
>>> for ele in r.getiterator(): # 所有的元素以及內容, 2.7以後用iter()代替了。
... print ele.tag
... if ele.text:
... print '-->%s' % str.strip(ele.text)
...
>>> r.find('subEle1').tail # 元素的尾巴,貌似元素之間沒人管的東東
" sub1's tails\n "
>>> r.getiterator('tag1') # 得到特定tag的元素,可以跨層,但是find不行。
[<Element tag1 at c9db70>, <Element tag1 at c9dbc0>, <Element tag1 at c9dc60>, <Element tag1 at c9dd
00>]
>>> print r.find('tag1')
None
>>> r.find('subEle2/tag1')
<Element tag1 at c9db70>
>>> r.findall('subEle2/tag1')
[<Element tag1 at c9db70>, <Element tag1 at c9dbc0>, <Element tag1 at c9dc60>, <Element tag1 at c9dd
00>]
------------------------------------------- 寫元素樹
n = ElementTree.ElementTree()
r = ElementTree.Element('root')
n._setroot(r)
>>> ElementTree.dump(n)
<root />
ElementTree.SubElement(r, 'subEle1', {'subAttrib' : "subAttrib1"})
t = ElementTree.Element("subEle2", {'subItem1' : 'ele21', 'subItem2' : 'ele22'})
t.text = "subEle2 text" # 屬性可以用set來修改。
r.append(t)
for i in range(1, 5):
tt = ElementTree.Element("tag1")
tt.text = "tag1's text%s" % i
t.append(tt)
t = r.find('subEle1')
t.tail = "subele1's tail"
n.write(r'c:\test.xml',"utf-8")
-------------------------------------------------
- <root>
<subEle1 subAttrib="subAttrib1" />
subele1's tail
- <subEle2 subItem1="ele21" subItem2="ele22">
subEle2 text
<tag1>tag1's text1</tag1>
<tag1>tag1's text2</tag1>
<tag1>tag1's text3</tag1>
<tag1>tag1's text4</tag1>
</subEle2>
</root>
---------------------------------------------------從文件中創建ElementTree
>>> x = ElementTree.ElementTree(file=r'c:\test.xml') # 從文件讀出
>>> ElementTree.dump(x)
>>> r = x.getroot() #得到根節點
>>> a = r.find('subEle1') # 找到子節點
>>> ElementTree.dump(a)
<subEle1 subAttrib="subAttrib1" />subele1's tail
>>> a.set('new', 'new1') # 設置新屬性
>>> ElementTree.dump(a)
<subEle1 new="new1" subAttrib="subAttrib1" />subele1's tail
>>> r.remove(a) # 刪除一個節點。。。
>>> a = r.find('subEle2') # 刪除1級節點下面的節點。。。
>>> for i in a.findall('tag1'):
... a.remove(i)
轉自:http://hi.baidu.com/derris/blog/item/e0e3b850e7abbe6f84352436.html
附上 PYTHON 官方網址:
關於ElementTree類的成員方法: