I am attempting to insert an element at a specific child node of a child node in a large XML document (~2000 lines) below is my code:
import xml.etree.cElementTree as ET
class Kapow_commands:
tree = ET.parse('location/of/xml/file')
root = tree.getroot()
seq_id = 39
def __init__(self):
pass
def append(self, block):
node_num=0
for node in Kapow_commands.root[13][1]:
node_num=node_num+1
if node.get('class') == 'End':
node.attrib['id'] = str(Kapow_commands.seq_id + 1)
print(node.attrib)
print(node_num)
Kapow_commands.root[13][1].insert(node_num -1, block)
block = ET.Element("test")
Kapow_commands().append(block)
this code scans the XML file at a specific node for class = 'End' then it adds 1 to that elements id and inserts another element before it. However when I run this it seems to create an infinity loop as it doesn't stop inserting elements at this position. Does anyone have any idea why this might be happening? Or any idea on how to properly insert this element at the desired position?