<?xml version="1.0" encoding="utf-8" ?> <!--this is a test about xml.--> <collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>
#練習:計算movie文件中有多少個名字叫War, Thriller的電影
import sys
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
tree = ET.parse("e:\\movie.xml")
count = 0
for elem in tree.iter(tag='movie'): #遍歷樹中的movie節點
print elem.tag
if elem[0].text == 'War, Thriller':
count += 1
print count
#以下代碼實現了邊讀文件邊解析的作用,節省了內存
count = 0
for event, elem in ET.iterparse("e:\\movie.xml"): #遍歷所有xml文件中的標簽
#print elem.tag
if event == 'end': #檢測“閉合的”(end)事件,標簽關閉
if elem.tag == 'type' and elem.text == 'War, Thriller': #標簽為type,且文本內容為War, Thriller ,則count+1
count += 1
elem.clear() #清除元素內容,不清除則整個兒樹也會在內存中,沒有起到節省內存的作用。
print count