def getChildTextbyParentAttribute (datafile, pnode, patt, pattval, cnode):
"""
Seraches XML file for the parent node with a specific value. Finds the child node and returns
its text
datafile = xml file searched
pnode = parent node
patt = parent node attribute
patval = parent node attribute value
cnode = child node
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
value = False
for node in root.findall(pnode):
attribute = node.get(patt)
if attribute == pattval:
cnode = node.find(cnode)
if cnode is not None:
value = cnode.text
else:
return None
break
return value
评论列表
文章目录