I\'ve looked through several posts but I haven\'t quite found any answers that have solved my problem.
Sample XML =
You should use the ElementTree api instead of minidom for your task (as explained in the other answers here), but if you need to use minidom, here is a solution.
What you are looking for was added to DOM level 3 as the textContent attribute. Minidom only supports level 1.
However you can emulate textContent pretty closely with this function:
def textContent(node):
if node.nodeType in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
return node.nodeValue
else:
return ''.join(textContent(n) for n in node.childNodes)
Which you can then use like so:
x = minidom.parseString("""
TEXT1 TEXT2 TEXT3 """)
twn = x.getElementsByTagName('TextWithNodes')[0]
assert textContent(twn) == u'\nTEXT1TEXT2 TEXT3'
Notice how I got the text content of the parent node TextWithNodes
. This is because your Node
elements are siblings of those text nodes, not parents of them.