I’m still deeply in love with ElementTree. Here’s a script that will give you all the lines of a given player. Note that the import area has changed, as on this machine I’m using Python 2.4:
#!/usr/bin/python
# Initialization
import elementtree.ElementTree as ET
import os, string
# Read in our XML file
infile = raw_input("Input XML file name > ")
xmltree = ET.ElementTree(file=infile)
rootelem = xmltree.getroot()
speaker_find = raw_input("Which player's lines do you wish? > ")
act_list = rootelem.findall('ACT')
for act in act_list:
scene_list = act.findall('SCENE')
for scene in scene_list:
speech_list = scene.findall('SPEECH')
for speech in speech_list:
speaker_list = speech.findall('SPEAKER')
for speaker in speaker_list:
if speaker.text == speaker_find.upper():
print speaker.text
lines = speech.findall('LINE')
for line in lines:
print line.text
print ''
and the output:
Input XML file name > hamlet.xml
Which player's lines do you wish? > osric
OSRIC
Your lordship is right welcome back to Denmark.
OSRIC
Sweet lord, if your lordship were at leisure, I
should impart a thing to you from his majesty.
OSRIC
I thank your lordship, it is very hot.
OSRIC
It is indifferent cold, my lord, indeed.
OSRIC
Exceedingly, my lord; it is very sultry,--as
'twere,--I cannot tell how. But, my lord, his
majesty bade me signify to you that he has laid a
great wager on your head: sir, this is the matter,--
OSRIC
Nay, good my lord; for mine ease, in good faith.
Sir, here is newly come to court Laertes; believe
me, an absolute gentleman, full of most excellent
differences, of very soft society and great showing:
indeed, to speak feelingly of him, he is the card or
calendar of gentry, for you shall find in him the
continent of what part a gentleman would see.
OSRIC
Your lordship speaks most infallibly of him.
OSRIC
Sir?
OSRIC
Of Laertes?
OSRIC
I know you are not ignorant--
OSRIC
You are not ignorant of what excellence Laertes is--
OSRIC
I mean, sir, for his weapon; but in the imputation
laid on him by them, in his meed he's unfellowed.
OSRIC
Rapier and dagger.
OSRIC
The king, sir, hath wagered with him six Barbary
horses: against the which he has imponed, as I take
it, six French rapiers and poniards, with their
assigns, as girdle, hangers, and so: three of the
carriages, in faith, are very dear to fancy, very
responsive to the hilts, most delicate carriages,
and of very liberal conceit.
OSRIC
The carriages, sir, are the hangers.
OSRIC
The king, sir, hath laid, that in a dozen passes
between yourself and him, he shall not exceed you
three hits: he hath laid on twelve for nine; and it
would come to immediate trial, if your lordship
would vouchsafe the answer.
OSRIC
I mean, my lord, the opposition of your person in trial.
OSRIC
Shall I re-deliver you e'en so?
OSRIC
I commend my duty to your lordship.
OSRIC
Ay, my good lord.
OSRIC
A hit, a very palpable hit.
OSRIC
Nothing, neither way.
OSRIC
Look to the queen there, ho!
OSRIC
How is't, Laertes?
OSRIC
Young Fortinbras, with conquest come from Poland,
To the ambassadors of England gives
This warlike volley.
It’s pretty great… 25 lines of code, and I’m still doing things in sort of a long way. For instance, I’ve already figured out how to rewrite the counting script from my previous post using the getiterator function:
#!/usr/bin/python
# Initialization
import elementtree.ElementTree as ET
import os, string
# Read in our XML file
infile = raw_input("Input XML file name > ")
xmltree = ET.ElementTree(file=infile)
rootelem = xmltree.getroot()
speaker_find = raw_input("Which player's lines do you wish? > ")
i = 0
speaker_list = xmltree.getiterator('SPEAKER')
for speaker in speaker_list:
if speaker.text == speaker_find.upper():
i = i + 1
print i
This steps through all the levels of the XML document for us, so there isn’t any need to do that manually. It’s facile to rewrite the “Player’s Lines” scripts similarly:
#!/usr/bin/python
# Initialization
import elementtree.ElementTree as ET
import os, string
# Read in our XML file
infile = raw_input("Input XML file name > ")
xmltree = ET.ElementTree(file=infile)
rootelem = xmltree.getroot()
speaker_find = raw_input("Which player's lines do you wish? > ")
speeches = xmltree.getiterator('SPEECH')
for speech in speeches:
speaker = speech.find('SPEAKER')
lines = speech.findall('LINE')
if speaker.text == speaker_find.upper():
print speaker.text
for line in lines:
print line.text
print ''
So far I’m still learning a lot with every poke at ElementTree. I’m sure I’ll be adding more as I go.
Hope you find this sort of thing interesting.