Parsing XML (simple)

Thomas Singleton singleton at skynet.be
Sat Oct 13 17:36:24 EDT 2001


Hello,
I'd like to parse a very simple xml doc which consists of a user/pass
and then a few commands.
Example :
<?xml version="1.0"?>
<commandmail>
<user>test</user>
<pass>password</pass>
<commands>
	<command>commande1</command>
	<command>commande2</command>
</commands>
</commandmail>

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.
The solution must be pretty simple but i couldn't find it myself.
Any help would be very appreciated !

Thanks in advance
Thomas

Here my program :

import xml.dom.minidom

dom = xml.dom.minidom.parse('test.xml')

def handleCommandMail(commandmail):
    print "Start"
    handleUserName(commandmail.getElementsByTagName("user")[0])
    handlePassword(commandmail.getElementsByTagName("pass")[0])
    commands = commandmail.getElementsByTagName("commands")
    for command in commands:
            handleCommand(command)
    print "Done"
                          
def handleCommand(command):
    commandline = command.getElementsByTagName("command")[0]
    print "Command : ", commandline.firstChild.data.strip()

def handleUserName(username):
    print "User : ", username.firstChild.data.strip()

def handlePassword(password):
    print "Pass : ", password.firstChild.data.strip()
    
handleCommandMail(dom)



More information about the Python-list mailing list