import xml.etree.ElementTree as etree #Load the xml file into memory. tree = etree.parse("orgchart.xml") #Get a pointer to the root node root = tree.getroot() #print the tag name of the root node. print(root.tag) #Iterate through all the root's children (CEO/President) for child in root: #print the name attribute from the attrib dictionary. print( " " + child.attrib['name']) #Print salary attribute (if it exists!) print( " -" + child.attrib.get('salary',"N/A") ) #Get a list of nodes with the name "vp" that are contained by #this child node. vps = child.findall("vp") #Note that vps will be an empty list for the CEO node, #but will contain 3 VP objects for the president node. #Iterate through the VP child nodes if the list isn't empty... for vp in vps: print(" " + vp.attrib['name']) print(" -" + vp.text)