def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value):
"""
This method takes an xml document as input and finds all the sub elements (parent/children)
containing specified tag and an attribute with the specified value.
Returns a list of matching elements.
Arguments:
datafile = input xml file to be parsed.
tag = tag value of the sub-element(parent/child) to be searched for.
attrib = attribute name for the sub-element with above given tag should have.
value = attribute value that the sub-element with above given tag, attribute should have.
"""
element_list = []
root = ElementTree.parse(datafile).getroot()
for element in root.iterfind(".//%s[@%s='%s']" % (tag, attrib, value)):
element_list.append(element)
return element_list
评论列表
文章目录