question about python "casting"

S

sang park

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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

sang said:
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.

Could it be that you have been executing a code different from
the one you were posting? In your posted code, you have a variable
called "proj", whereis from your exception, it appears the variable
is called "p".
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

It is unlikely that this does what you want: Most likely, you
have elements "name", "email", etc in your XML document.
You need to look at some node's nodeName, to find out whether
it is a "name" node - looking at the text inside the node
won't help.

E.g. in your code above, if dataStr equals "email", you do

project.email = dataStr

Since dataStr is email, this is the same as

project.email = "email"

which is not what you want.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top