Function returns none

2005-10-31 Thread noahlt
I'm trying to write a website updating script, but when I run the
script, my function to search the DOM tree returns None instead of what
it should.

I have this program:

import sys
from xml.dom.minidom import parse


# search the tree for an element with a particular class

def findelement(current, classtofind, topnode = None):
if topnode == None: topnode = current



# if it's an xml element...
if current.nodeType == 1:
print current.nodeName, ':', current.getAttribute('class')
if current.getAttribute('class') == classtofind:
print 'Returning node:', current
return current
elif current.hasChildNodes():
findelement(current.firstChild, classtofind, topnode)
elif current.nextSibling:
findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

 and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None

# others (text, comment, etc)

else:

if current.nextSibling:

findelement(current.nextSibling, classtofind, topnode)

elif (current.parentNode != topnode) \

 and (current.parentNode.nextSibling != None):

findelement(current.parentNode.nextSibling, classtofind,
topnode)
else:

print 'Returning None...'

return None



# parse the document

blog = parse('/home/noah/dev/blog/template.html')



# find a post

postexample = findelement(blog.documentElement, 'post')



print 'Got node:   ', postexample

-

My output is this:

-
html :
head :
title :
body :
h1 :
ul :
li :
h2 :
ol :
li : post
Returning node: 
Got node:None
-

The function finds the right element fine, and says it will return , but the program gets None instead.  What's
happening here?  Any suggestions?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Function returns none

2005-10-31 Thread noahlt
Thanks, that worked!

-- 
http://mail.python.org/mailman/listinfo/python-list


XML DOM: XML/XHTML inside a text node

2005-11-02 Thread noahlt
In my program, I get input from the user and insert it into an XHTML
document.  Sometimes, this input will contain XHTML, but since I'm
inserting it as a text node, xml.dom.minidom escapes the angle brackets
('<' becomes '<', '>' becomes '>').  I want to be able to
override this behavior cleanly.  I know I could pipe the input through
a SAX parser and create nodes to insert into the tree, but that seems
kind of messy.  Is there a better way?

Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list