I am trying to use xml.etree.ElementTree to parse a xml file, find a specific tag, append a child to that tag, append another child to the newly created tag and add text to the
I prefer to define my own function for adding text:
def SubElementWithText(parent, tag, text):
attrib = {}
element = parent.makeelement(tag, attrib)
parent.append(element)
element.text = text
return element
Which then is very convenient to use as:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
a = root.find('a')
b = ET.SubElement(a, 'b')
c = SubElementWithText(b, 'c', 'text3')