I can open and read an XML file.
What I'd like to do is a recursion to print out all child elements of the current node.
The (awful) iterative approach has nested for loops:
root = tree.getroot()
for child in root:
print (child.tag, child.text)
for gen1 in child:
print(child.tag, "\t" , gen1.tag, "\t\t", gen1.text)
for gen2 in gen1:
print(child.tag, "\t" , gen1.tag, "\t" ,gen2.tag, "\t\t", gen2.text)
for gen3 in gen2:
print(child.tag, "\t" , gen1.tag, "\t" , gen2.tag, "\t", gen3.tag, "\t\t", gen3.text)
for gen4 in gen3:
print(child.tag, "\t" , gen1.tag, "\t" , gen2.tag, "\t", gen3.tag, "\t", gen4.tag, "\t\t", gen4.text)
for gen5 in gen4:
print(child.tag, "\t" , gen1.tag, "\t" , gen2.tag, "\t", gen3.tag, "\t", gen4.tag, "\t", gen5.tag, "\t\t", gen5.text)
for gen6 in gen5:
print(child.tag, "\t" , gen1.tag, "\t" , gen2.tag, "\t", gen3.tag, "\t", gen4.tag, "\t", gen5.tag, "\t", gen6.tag, "\t\t", gen6.text)
for gen7 in gen6:
print(child.tag, "\t" , gen1.tag, "\t" , gen2.tag, "\t", gen3.tag, "\t", gen4.tag, "\t", gen5.tag, "\t", gen6.tag, "\t", gen7.tag, "\t\t", gen7.text)
I can't wrap my head around "this node has no children" for the stop of recursion. Any suggestions?