[XML-SIG] Re: [4suite] dom question

Alexandre Fayolle Alexandre.Fayolle@logilab.fr
Tue, 16 Oct 2001 08:59:49 +0200 (CEST)


On Tue, 16 Oct 2001, markus jais wrote:

> hello
> I took the following programm out of a python book
> (slightly modified)
> ------------------
> #!/usr/local/bin/python
> 
> from xml.dom.ext.reader import Sax

The prefered module, I think, is xml.dom.ext.reader.Sax2

> from xml.dom import Node
> s="""<languages>
> <lang><name>Perl</name><kritik>good</kritik></lang>
> <lang><name>Python</name><kritik>great</kritik></lang>
> <lang><name>Ruby</name><kritik>great</kritik></lang>
> </languages>
> """
> def text_in_children(n):
> 	t = ""
> 	for c in n.childNodes:
> 		if c.nodeType == Node.TEXT_NODE:
> 			t += c.nodeValue
> 	return t
> 
> baum = Sax.FromXml(s)
> languages = baum.getElementsByTagName("languages")[0]
> 
> for lang in languages.childNodes:
> 	if lang.nodeName == "lang":
> 		for c in lang.childNodes:
> 			#if c.hasAttributes():  ########### does not work!!!!
> 			#	print "has attributs"
> 			if c.nodeName == "name":
> 				print text_in_children(c.nextSibling)
> print "done"
> ----------------------------
> 
> I played a little bit with this file and I have some questions?
> 
> 1)
> according to the dom documentation in the python library reference
> there should be a method for Node, but I get the following
> error, when I call the method in the above code:
>     AttributeError: class FtNode has no attribute 'hasAttributes'  
> is the documentation wrong, or is it my mistake????

This method is not implemented in 4DOM. On the other hand, only Element
nodes have attributes. You can also use the attributes member variable, so
your test would be (not tested):

if c.attributes:
    print 'has attributes'


> 
> 2)
> can anybody tell me, where in the sources of 4DOM
> the method "childNodes" is implemented.
> I have been trying more to find the method in the sources
> for more than an hour now.
> maybe I am just blind :-)

in FtNode, line 103. It is not a method, rather a direct attribute access.

> I really appreciate any hints!!!!

Well since you asked, 4Thought gave the source code of 4DOM to the PyXML
Project, so the right place to ask question on 4DOM is xml-sig@python.org
(cc'ed to this mail). Lots of people have subscribed to both lists, so you
should get answers on both anyway. 

Alexandre Fayolle
-- 
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
Narval, the first software agent available as free software (GPL).