mindthief
mindthief

Reputation: 13383

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

I've called elems = xmldoc.getElementsByTagName('myTagName') on an XML object that I parsed as minidom.parse(xmlObj). Now I'm trying to get the text content of this element, and although I spent a while looking through the dir() and trying things out, I haven't found the call yet. As an example of what I want to accomplish, in:

<myTagName> Hello there </myTagName>

I would like the extract just "Hello there". (obviously I could parse this myself but I expect there is some built-in functionality)

Thanks

Upvotes: 16

Views: 27483

Answers (3)

mike rodent
mike rodent

Reputation: 15682

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

Upvotes: 8

James Thompson
James Thompson

Reputation: 65

for elem in elems:
    print elem.firstValue.nodeValue

That will print out each myTagName's text.

James

Upvotes: 2

ismail
ismail

Reputation: 47642

Try like this:

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

Upvotes: 30

Related Questions