1. from xml.etree import ElementTree as ET
    2. content = """
    3. <data>
    4. <country name="Liechtenstein">
    5. <rank>2</rank>
    6. <year>2023</year>
    7. <gdppc>141100</gdppc>
    8. <neighbor direction="E" name="Austria" />
    9. <neighbor direction="W" name="Switzerland" />
    10. </country>
    11. <country name="Panama">
    12. <rank>69</rank>
    13. <year>2026</year>
    14. <gdppc>13600</gdppc>
    15. <neighbor direction="W" name="Costa Rica" />
    16. <neighbor direction="E" name="Colombia" />
    17. </country>
    18. </data>
    19. """
    20. root = ET.XML(content)
    21. for child in root.iter('year'):
    22. print(child.tag, child.text)
    23. # 输出:
    24. # year 2023
    25. # year 2026