I’ve got a function to return a list from an etree element, but it doesn’t look through nested elements.
<elem>
<variable id="getthis">
<!-- / -->
</variable>
<if>
<variable id="alsoGetThis">
<!-- Keep looping through all elements -->
</variable>
</if>
</elem>
(I’m working with Valid XML)
So currently the variable within the <if> is ignored, so how can you loop through all levels of the tree? I’m assuming this is a simple task, but maybe I’m wrong. (I’m new to Python & don’t always think like a programmer)
Python func to get the variables:
def collect_vars(self, elem):
elemVars = []
if elem.tag == 'variable':
elemVars.append(elem.attrib['id'])
elif e in elem == 'variable': # don't want to be doing these
elemVars.append(e.attrib['id'])
return elemVars
So all I want to end up with is the list elemVars containing all the variable IDs within the given <elem>
The problem you are facing is that you are not visiting all nodes in the file. You are only visiting the children of the
elemelement, but you are not visiting the children of these elements. To illustrate this, running the following (I have edited your XML to be valid):results in
So you are not visiting the child
variableof the nodeif. You will need to recursively visit each node in your XML file, i.e. you functioncollect_varswill need to call itself. I’ll post some code in a bit to illustrate this.Edit: As promised, some code to get all
idattributes from your element tree. Rather than using an accumulator as Niek de Klein has I have used a generator. This has a number of advantages. For example, this returns theids one at a time, so you can stop processing at any point, if, for example, a certainidis encountered, which saves reading the entire XML file.This yields the result