1 import xml.etree.ElementTree as ET 2 tree = ET.parse('E:\\test\\xx.xml') 3 root = tree.getroot() 4 print(root) 5 6 for city in root.iter('city'): 7 print(city.get('cityName'),city.get('quName'))
假设处理xml文件如下:xx.xml
<china dn = "nay">
<city quName="heikongjiang" pyName="heihei" cityName="ha2ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha2ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha3ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha3ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha4ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha1ging"/>
<city quName="heikongjiang" pyName="heihei" cityName="ha2ing"/>
</china>
怕什么真理无穷,进一寸有一寸的欢喜。---胡适