Python ElementTree - Inserting a copy of an element

hodgrelyscur

New Member
I have the following xml code:\[code\]<data factor="1" name="ini" value="http://stackoverflow.com/questions/15527399/342" />\[/code\]I want to copy the same information, but with a different name. i.e., The final output should be:\[code\]<data factor="1" name="ini" value="http://stackoverflow.com/questions/15527399/342" /><data factor="1" name="raw_ini" value="http://stackoverflow.com/questions/15527399/342" />\[/code\]I tried to do the following:\[code\]model_tag = tree.findall(data_path) #I make sure that data_path is correct.len_tags = len(model_tag)i = 0while i < len_tags: tipo_tag = model_tag if tipo_tag.attrib['name']=='ini': aux_tag = copy.deepcopy(tipo_tag) #I tried also with copy.copy(tipo_tag). aux_tag.attrib['name'] = 'raw_ini' model_tag.append(aux_tag)tree.write(dir_output) \[/code\]If I use "copy.deepcopy" I don't have an extra element. The output is:\[code\]<data factor="1" name="ini" value="http://stackoverflow.com/questions/15527399/342" />\[/code\]If I use "copy.copy", just change the name of the element. The output is:\[code\]<data factor="1" name="raw_ini" value="http://stackoverflow.com/questions/15527399/342" />\[/code\]Any Idea of what am I doing wrong?
 
Back
Top