from xml.etree import ElementTree as ETcontent = """<data> <country name="Liechtenstein"> <rank>2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Panama"> <rank>69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country></data>"""# 获取根标签 dataroot = ET.XML(content)# 获取data标签的孩子标签for child in root: # child.tag = conntry # child.attrib = {"name":"Liechtenstein"} print(child.tag, child.attrib) for node in child: print(node.tag, node.attrib, node.text)