from xml.dom.minidom import parseString
dom = parseString("""<message>
  <text>
    Hello!
  </text>
</message>""")
dom.childNodes[0].childNodes
text = dom.getElementsByTagName('text')[0].childNodes[0]
text.data = text.data.replace('Hello', 'Hello world')
Alter XML while preserving layout