question about python "casting"

sang park sang at bittersoft.com
Mon Feb 9 00:28:24 EST 2004


Hi all -  Just started learning Python this past week and had a question
about using the lists in python with objects.  (code attached below).

basically, what i want to do is read an xml file, and load a list of Objects
(in this case, TTSProject objects).  the problem i see when i try to run it,
is the following:

name :
Traceback (most recent call last):
  File "ProjectParser.py", line 63, in ?
    print "name : ", p.name
AttributeError: 'NoneType' object has no attribute 'name'

i was wondering what the proper way to accomplish this would be.  any help
would be appreciated.

thanks,

sang



from xml.dom import minidom

print "Starting"
xmldoc = minidom.parse('projects.xml')
print "Getting the root node"
root = xmldoc.childNodes
projectlist = root[0].childNodes
print "project list node"


class TTSProject:
    def __init__(self):
	    self.tags = {};

projects = []

def getText(nodes):
    for node in nodes:
        if node.nodeType == node.TEXT_NODE:
            return node.data

def getProject (nodes):
    project = TTSProject()
    if nodes.nodeType == nodes.ELEMENT_NODE:
        datanodes = info.childNodes
        dataStr = getText(datanodes)
#        print info.nodeName, ":", dataStr
        if dataStr == "name":
            project.name = dataStr
        if dataStr == "email":
            project.email = dataStr
        if dataStr == "admin":
            project.admin = dataStr
        if dataStr == "description":
            project.description = dataStr
    return project

for p in projectlist:
    projectInfo = p.childNodes
    for info in projectInfo:
        project = getProject(info)
        projects.append(project)

for proj in projects:
    print "name : ", proj.name
    print "email : ", proj.email
    print "admin : ", proj.admin
    print "description : ", proj.description





More information about the Python-list mailing list