简体   繁体   中英

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

试试这样:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM