![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import xml.etree.ElementTree as ET tree = ET.parse("xml_test.xml") root = tree.getroot() print(root) print(root.tag) # 遍历xml文档 for child in root: print(child.tag, child.attrib) for i in child: print(i.tag, i.text,i.attrib)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import xml.etree.ElementTree as ET tree = ET.parse("xml_test.xml") root = tree.getroot() for node in root.iter('year'): print(node.tag, node.text)