Parsing XML (simple)

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Oct 15 10:47:39 EDT 2001


singleton at skynet.be (Thomas Singleton) writes:

> <commands>
> 	<command>commande1</command>
> 	<command>commande2</command>
> </commands>
[...]
> After reading the modules doc and a few usenet posts, i have written a
> small python program to echo the values contained between tags, but
> i'm having troubles with the commands, only the FIRST one is echoed.

This is no surprise. You do

>     commands = commandmail.getElementsByTagName("commands")
>     for command in commands:
>             handleCommand(command)

This gives you all "commands" elements. Since there is only a single
"commands" element, this loop is executed just once. Then you do

> def handleCommand(command):
>     commandline = command.getElementsByTagName("command")[0]
>     print "Command : ", commandline.firstChild.data.strip()

This gets all "command" children of the "commands" element, then takes
only the first of those "command" elements - ignoring the second.

Regards,
Martin



More information about the Python-list mailing list