import xml.etree.ElementTree as etree #Create a variable that points to the root Element rootVar = etree.Element("orgchart") #Create the CEO node and attach it as a child of the root ceoNode = etree.Element("ceo", name="Vicky") rootVar.append(ceoNode) #Create the president node and fill in some data. presNode = etree.Element("president") presNode.attrib["name"] = "Herbet T. Walker, III" presNode.attrib["salary"] = "$234,000" presNode.text="Make Money!" rootVar.append(presNode) #Create 3 VP nodes from a list of data: data = [ ("Marketing", "Sally", "Get Customers!"), ("Sales", "Tom", "Make Sales!"), ("Production", "Cindy", "Build Widgets!") ] #Dynamicly generate one VP element for each tuple in the list! for VP in data: typeStr = VP[0] nameStr = VP[1] motoStr = VP[2] vpNode = etree.SubElement(presNode, "vp", name=nameStr, vptype=typeStr) vpNode.text = motoStr #Create a tree from the root of our XML hierarchy and write the tree ourTree = etree.ElementTree(rootVar) ourTree.write("orgchart.xml", "UTF-8") #Always use UTF-8