0
我在python3中使用ElementTree XML API并且有一个问题,这似乎很基础,我只是没有在文档中找到正确的函数它。起点是我认为一个xml文件,其名称在字符串name
中给出。我正在寻找一个函数来检查一个chld是否存在。由它的名字。目前,我在做什么是:在python中检查是否存在使用ElementTree进行XML解析的孩子
import xml.etree.ElementTree as ET
tree = ET.parse(name)
root = tree.getroot()
item = root.getchildren()[2]
,因为我知道,我期待在该项目是在位置2(第3项)。但我宁愿有这样的事情:
item = root.checkIfExists('itemName')
有人可以为此提出一个函数吗?或者更好的方法来解决这个问题?谢谢。