在xml文件中获取元素子元素的Python方式是什么

在xml文件中获取元素子元素的Python方式是什么,python,xml-parsing,Python,Xml Parsing,我尝试了以下方法: tree = ET.parse(latestSumocfg) root = tree.getroot() for child in root: if child.tag == "input": for granchild in child: print(granchild.get('value')) 它可以工作,但我相信有一个更优雅的解决方案,你可以使用findall: tree = ET.parse(latestSumocf

我尝试了以下方法:

tree = ET.parse(latestSumocfg)
root = tree.getroot()
for child in root:
    if child.tag == "input":
        for granchild in child:
            print(granchild.get('value'))

它可以工作,但我相信有一个更优雅的解决方案,你可以使用
findall

tree = ET.parse(latestSumocfg)
for grandchild in tree.findall('input/*'):
    print(granchild.get('value'))
或者如果您使用
lxml的ElementTree实现:

tree = lxml.etree.parse(latestSumocfg)
for value in tree.xpath('input/*/@value'):
    print(value)