For your special requirement, I changed it so that each iteration works with the parent and the child. The output is based on the parent - in that way the parent automatically is output k-times. It also needs a special case to output the child when there are no further children.
from itertools import izip
import xml.etree.ElementTree as ET
def main():
prefix = "{http://jbpm.org/4.4/jpdl}"
xml = ET.parse("testWF2.xml")
root = xml.getroot()
i = root.findall('*')
# convert list to dictionary indexed by Element.name
temp = []
for it in i:
name = it.get("name")
if name:
temp.append(name)
else:
tag = it.tag
temp.append(tag.replace(prefix, '')) # if no name exists use tag (ex. start and end)
b = dict(izip(temp, i)) # create the dictionary with key = name
nodes = []
# add root to the list
start_pair = (None, b["start"]) # # # # # using pairs
nodes.append(start_pair)
while(nodes):
parent, n = nodes.pop() # # # # # using pairs
transitions = n.findall(prefix+"transition")
children = []
# get all of n's children
for t in transitions:
child = b[t.get("to")]
children.append(child)
nodes.append((n, child)) # add parent/child pair
# only output the parent (thus outputing k times)
try:
print parent.get("name", "start")
except AttributeError:
pass # ignore the start position
# also output the node if it has no children (terminal node)
if len(children) < 1:
print n.get("name", "start")
# end while loop
main()
start
a
b
d
f
end
d
c
e
end
b
c
e
end