Python: How do you get an XML element's text content using xml.dom.minidom? Python: How do you get an XML element's text content using xml.dom.minidom? xml xml

Python: How do you get an XML element's text content using xml.dom.minidom?


Try like this:

xmldoc.getElementsByTagName('myTagName')[0].firstChild.nodeValue


wait a mo... do you want ALL the text under a given node? It has then to involve a subtree traversal function of some kind. Doesn't have to be recursive but this works fine:

    def get_all_text( node ):        if node.nodeType ==  node.TEXT_NODE:            return node.data        else:            text_string = ""            for child_node in node.childNodes:                text_string += get_all_text( child_node )            return text_string


for elem in elems:    print elem.firstValue.nodeValue

That will print out each myTagName's text.

James