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

mindthief picture mindthief · Dec 19, 2010 · Viewed 24.5k times · Source

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

Answer

ismail picture ismail · Dec 19, 2010

Try like this:

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