By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

question about python "casting"

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sang park wrote:
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.