[XML-SIG] minidom: read value from text node
Daniel Isenegger
disen at geo.unizh.ch
Mon Dec 13 17:46:22 CET 2004
Hi,
i work with the PyXML modul an use minidom to process xml data:
In need to get data from text nodes and found an example of
functionality in the book O'Reilly-book from C.Jones: Python & XML (S.101)
# 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
Output:
text-node: 1
text-node:
cat:
my Problem is now that
the function findTextNodes now is a recursive one, which delivers 2
return -values:
the correct value of the text-node and a non-empty string (it looks
empty but has 7 spaces )
How can i only get the correct text-value back or how can prevent the
string to overwrite the correct value?
( i tried to do this by only to return if subnode.data.isdigit() ist
true and othter workarounds, but...(:( )
Thanks Dani
More information about the XML-SIG
mailing list