"William O'Higgins Witteman" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>I am running a program (via py2exe) that is reading some XML files
>with
> xml.minidom. I am getting an error wherein I pull a value from a
> tag
> which (I think) happens to be empty. Thus, it throws this error:
>
> AttributeError: 'NoneType' object has no attribute 'data'
>
> Here's the code that creates this problem:
>
> def functionname(fileobject):
> try:
> xmldoc = minidom.parse(fileobject)
> except xml.parsers.expat.ExpatError, AttributeError:
> logit = "There is a malformed file: " + fileobject + "\n"
> logfile.write(logit)
> else:
> a = xmldoc.getElementsByTagName('date_modified')
> try:
> b = a[0].firstChild.data
> except xml.parsers.expat.ExpatError, AttributeError:
> logit = "There is a malformed file: " + fileobject + "\n"
> logfile.write(logit)
>
> I am wondering what I have to do to catch this exception - I'm
> assuming
> that the problem is that "a" is an empty object, and so it has not
> attributes. Thanks.
I'm not sure what your problem is but you could simplify the
code by using a single try/except pair here, there is no advantage
to using two as you show here.(At least i can't think of any)
Your code would thus become:
def functionname(fileobject):
try:
xmldoc = minidom.parse(fileobject)
a = xmldoc.getElementsByTagName('date_modified')
b = a[0].firstChild.data
except xml.parsers.expat.ExpatError, AttributeError:
logit = "There is a malformed file: " + fileobject + "\n"
logfile.write(logit)
But I might also put an 'if' check in the code:
a = xmldoc.getElementsByTagName('date_modified')
if a and a.firstChild: b = a[0].firstChild.data
else: print 'no date_modified' # or set a default or whatever
but then you would expect the except clause to pick that up.
Sorry I can't be more help.
Alan G.
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor