その場で変更するよりも、XML ファイルを再生成する方が簡単です。
from xml.dom.minidom import Document
doc = Document( )
root = doc.createElement( "root" )
for key, value in <some iterator>:
message = doc.createElement( "message" )
source = doc.createElement( "Source" )
source.appendChild( doc.createTextNode( key ) )
dest = doc.createElement( "Destination" )
dest.appendChild( doc.createTextNode( value ) )
message.appendChild( source )
message.appendChild( dest )
root.appendChild( message )
doc.appendChild( root )
print( doc.toprettyxml( ) )
これは印刷されます:
<root>
<message>
<Source>
key
</Source>
<Destination>
value
</Destination>
</message>
</root>
たとえばconfigparser
、ファイルを読み取るために使用できます。もっと良い方法があるかもしれません。