#XML Review Example #This code writes data to an XML file. See the #corresponding example_xml_reader.py file for #reading the data back in and accessing it. #Copyright Jay Summet 2010 import xml.etree.ElementTree as etree root = etree.Element("Root") #In the first example, we create a subelement of the #root node, which attaches it to the root node when #it is created. person = etree.SubElement(root, "Person", name="Bob") age1 = etree.SubElement(person, "Age") age1.text = "18" gender1 = etree.SubElement(person, "Gender") gender1.text = "M" #In the second example, we create a free standing #element, and then later attach (append) it to the root #node. person = etree.Element("Person", name="Sara") age1 = etree.SubElement(person, "Age") age1.text = "22" gender1 = etree.SubElement(person, "Gender") gender1.text = "F" root.append(person) #We add the root to a tree and write it out to a file: tree = etree.ElementTree(root) tree.write("exampleOUT.xml", "UTF-8")