Writing to a xml file using pythons element tree
Question: I'm using python's Element tree library to parse/write to an xml file:

    
    
    <?xml version='1.0' encoding='utf-8'?>
        <data>
          <reminder id="9">
            <enabled>true</enabled>
            <programmename>Show Me Show Me</programmename>
            <starttime>10:05</starttime>
            <sdate>Tuesday, 17 November 2015</sdate>
            <channel>NA</channel>
          </reminder>
        </data>
    

I'm using the following to write this out:

    
    
    from xml.etree import ElementTree as ET
    
            data = ET.Element("data")
            reminder = ET.SubElement(data, "reminder")
            reminder.set("id", "9")
            enabled = ET.SubElement(reminder, "enabled")
            enabled.text = "true"
            programmename = ET.SubElement(reminder, "programmename")
            programmename.text = new_programmename
            starttime = ET.SubElement(reminder, "starttime")
            starttime.text = new_programmetime
            sdate = ET.SubElement(reminder, "sdate")
            sdate.text = new_programmedate
            channel = ET.SubElement(reminder, "channel")
            channel.text = "NA"
            indent(data, level=0)
            tree = ET.ElementTree(data)
            tree.write(xmlfile, xml_declaration=True, encoding='utf-8', method="xml")
    

Although I'm struggling to understand how to add an additional
"record"/element? for example, so the xml looks like this:

    
    
    <?xml version='1.0' encoding='utf-8'?>
    <data>
    <reminder id="9">
        <enabled>true</enabled>
        <programmename>Show Me Show Me</programmename>
        <starttime>10:05</starttime>
        <sdate>Tuesday, 17 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
    <reminder id="10">
        <enabled>true</enabled>
        <programmename>foo</programmename>
        <starttime>13:05</starttime>
        <sdate>Wednesday, 18 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
    <data>
    

Any assistance would be greatly appreciated, i cant find any reference to this
or rather reference that i can understand...


Answer: Try as below-

    
    
    from lxml import etree as et
    
    
    xml="""<?xml version='1.0' encoding='utf-8'?>
        <data>
          <reminder id="9">
            <enabled>true</enabled>
            <programmename>Show Me Show Me</programmename>
            <starttime>10:05</starttime>
            <sdate>Tuesday, 17 November 2015</sdate>
            <channel>NA</channel>
          </reminder>
        </data>"""
    
    tree = et.fromstring(xml)
    element = tree.find(".//reminder[@id='9']")
    element_parent  = element.getparent()
    element_parent.insert(element_parent.index(element)+1,et.XML("""
    
    <reminder id="10">
        <enabled>true</enabled>
        <programmename>foo</programmename>
        <starttime>13:05</starttime>
        <sdate>Wednesday, 18 November 2015</sdate>
        <channel>NA</channel>
    </reminder>"""))
    
    print(et.tostring(tree))
    

Prints-

    
    
    <data>
          <reminder id="9">
            <enabled>true</enabled>
            <programmename>Show Me Show Me</programmename>
            <starttime>10:05</starttime>
            <sdate>Tuesday, 17 November 2015</sdate>
            <channel>NA</channel>
          </reminder>
          <reminder id="10">
            <enabled>true</enabled>
            <programmename>foo</programmename>
            <starttime>13:05</starttime>
            <sdate>Wednesday, 18 November 2015</sdate>
            <channel>NA</channel>
          </reminder>
    </data>
    

acknowledge "unutbu"

