[XML-SIG] minidom: read value from text node

Chuck Bearden cfbearden at gmail.com
Mon Dec 13 18:36:39 CET 2004

> # searches the year and the defoliation value in the instance file and
> copies the
> # element defoliation
>     def copyDynamicProperties(self):
>         """copies the dyn properties from a GML3 instance file."""
>         # looks for elements with the tag defol
>         defolNodelist = self.dom.getElementsByTagName("defol")
>         # iterating over all <defol>-eles
>         for node in defolNodelist:
>             # node list defolChildren
>             defolChildren = node.childNodes
>             # reads value cat in instance-file to member var self.cat
>             cat = self.findTextNodes(defolChildren,"cat")
>             print cat
>     #------------------------------------------------------------
>     # finds the data of the textNode of the elementNode with the
>     # tag theTagName (adapted from Book Python&XML, S.101)
>     def findTextNodes(self,theNodeList,theTagName):
>         #init returnValue
>         for subnode in theNodeList:
>             if subnode.nodeType == subnode.ELEMENT_NODE:
>                 if subnode.tagName == theTagName:
>                     # call function again to get children
>                     self.findTextNodes(subnode.childNodes,theTagName)
>             #
>             elif subnode.nodeType == subnode.TEXT_NODE:
>                 print "text-node:", subnode.data
>                 return subnode.data

My suspicion is that 'cat' is just getting the first whitespace
encountered by findTextNodes as it processes the list of child nodes
passed to it.  Notice that the only return statement in findTextNodes
is in the branch that handles child nodes that are text.  This means
at least two things: (1) when iterating "for subnode in theNodeList",
iteration will stop at the first child noted of type TEXT_NODE.  No
further nodes of any type in theNodeList will be processed; (2)
nothing that happens in recursive calls of findTextNodes (under the
"if subnode.nodeType == subnode.ELEMENT_NODE:" conditional branch) is
ever returned.

It can be tricky to mix iteration and recursion in this way.  However
you resolve this problem, you probably need an accumulator (e.g. a
list to which the content of all TEXT_NODEs under defol elements is
appended, to be joined and stripped when complete).  The accumulator
would be passed down the recursion chain as an argument to the
recurring function, and it would be returned back up the chain, and
eventually assigned to 'cat', where it would be .join()ed and

Best wishes,

More information about the XML-SIG mailing list