import xml.etree.ElementTree as ET # Load an XML document and get the root element tree = ET.parse("Macbeth.xml") root = tree.getroot() # Find all Persona persona = root.findall(".//PERSONA") print("All Persona") for per in persona: print(" %s" % per.text) #Get all the titles for all the scenes scenes = root.findall("ACT/SCENE/TITLE") print("All Scene Titles") for titles in scenes: print(" %s" % titles.text) #Get the speech with most lines print("Longest Speech in the scene") Largest = -10 LargestSpeech = "" for speech in root.iter('SPEECH'): speechLength = 0 speechText = "" for lines in speech.iter('LINE'): try: speechLength += len(lines.text) speechText += " " + str(lines.text) + "\n" except TypeError: pass if speechLength > Largest: Largest = speechLength LargestSpeech = speechText print("%s" % LargestSpeech) #Update the XML File print("Add an XML comment to the file: See output.xml") for stageDir in root.iter('STAGEDIR'): StageDirText = stageDir.text + "" stageDir.text = StageDirText tree.write('output.xml')