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. # 获取根标签 data
    21. root = ET.XML(content)
    22. # 获取data标签的孩子标签
    23. for child in root:
    24. # child.tag = conntry
    25. # child.attrib = {"name":"Liechtenstein"}
    26. print(child.tag, child.attrib)
    27. for node in child:
    28. print(node.tag, node.attrib, node.text)