how to replace elements in xml using python
<p>sorry for my poor English. but i need your help  ;( </p>

<p>i have 2 xml files.</p>

<p>one is:</p>

<pre><code>&lt;root&gt;
&lt;data name="aaaa"&gt;
&lt;value&gt;"old value1"&lt;/value&gt;
&lt;comment&gt;"this is an old value1 of aaaa"&lt;/comment&gt;
&lt;/data&gt;
&lt;data name="bbbb"&gt; 
&lt;value&gt;"old value2"&lt;/value&gt;
&lt;comment&gt;"this is an old value2 of bbbb"&lt;/comment&gt;
&lt;/data&gt;
&lt;/root&gt;
</code></pre>

<p>two is:</p>

<pre><code>&lt;root&gt;
&lt;data name="aaaa"&gt;
&lt;value&gt;"value1"&lt;/value&gt;
&lt;comment&gt;"this is a value 1 of aaaa"&lt;/comment&gt;
&lt;/data&gt;
&lt;data name="bbbb"&gt; 
&lt;value&gt;"value2"&lt;/value&gt;
&lt;comment&gt;"this is a value2 of bbbb"&lt;/comment&gt;
&lt;/data&gt;
&lt;data name="cccc"&gt; 
&lt;value&gt;"value3"&lt;/value&gt;
&lt;comment&gt;"this is a value3 of cccc"&lt;/comment&gt;
&lt;/data&gt;
&lt;/root&gt;
</code></pre>

<p>one.xml will be updated from two.xml.</p>

<p>so, the one.xml should be like this.</p>

<p>one.xml(after) :</p>

<pre><code>&lt;root&gt;
&lt;data name="aaaa"&gt;
&lt;value&gt;"value1"&lt;/value&gt;
&lt;comment&gt;"this is a value1 of aaaa"&lt;/comment&gt;
&lt;/data&gt;
&lt;data name="bbbb"&gt; 
&lt;value&gt;"value2"&lt;/value&gt;
&lt;comment&gt;"this is a value2 of bbbb"&lt;/comment&gt;
&lt;/data&gt;
&lt;/root&gt;
</code></pre>

<p>data name="cccc" is not exist in one.xml. therefore ignored.</p>

<p>actually what i  want to do is </p>

<ol>
<li>download two.xml(whole list) from db</li>
<li>update my one.xml (it contains DATA-lists that only the app uses) by two.xml</li>
</ol>

<p>Any can help me please !! 
Thanks!!</p>

<p>==============================================================
xml.etree.ElementTree<br>
your code works with the example. but i found a problem in real xml file.</p>

<p>the real one.xml contains :</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;root&gt;
    &lt;resheader name="resmimetype"&gt;
        &lt;value&gt;text/microsoft-resx&lt;/value&gt;
    &lt;/resheader&gt;
    &lt;resheader name="version"&gt;
        &lt;value&gt;2.0&lt;/value&gt;
    &lt;/resheader&gt;
    &lt;resheader name="reader"&gt;
        &lt;value&gt;System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/value&gt;
    &lt;/resheader&gt;
    &lt;resheader name="writer"&gt;
        &lt;value&gt;System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/value&gt;
    &lt;/resheader&gt;

    &lt;data name="NotesLabel" xml:space="preserve"&gt;
        &lt;value&gt;Hinweise:&lt;/value&gt;
        &lt;comment&gt;label for input field&lt;/comment&gt;
    &lt;/data&gt;
    &lt;data name="NotesPlaceholder" xml:space="preserve"&gt;
        &lt;value&gt;z . Milch kaufen&lt;/value&gt;
        &lt;comment&gt;example input for notes field&lt;/comment&gt;
    &lt;/data&gt;
    &lt;data name="AddButton" xml:space="preserve"&gt;
        &lt;value&gt;Neues Element hinzufÃ¼gen&lt;/value&gt;
        &lt;comment&gt;this string appears on a button to add a new item to the list&lt;/comment&gt;
    &lt;/data&gt;
&lt;/root&gt;
</code></pre>

<p>it seems, resheader causes trouble.
do you have any idea to fix? </p>

<p>You can use <a href="https://docs.python.org/3/library/xml.etree.elementtree.html" rel="nofollow">xml.etree.ElementTree</a> and while there are propably more elegant ways, this should work on files that fit in memory if <code>name</code>s are unique in <code>two.xml</code></p>

<pre><code>import xml.etree.ElementTree as ET
tree_one = ET.parse('one.xml')
root_one = tree_one.getroot()
tree_two = ET.parse('two.xml')
root_two = tree_two.getroot()

data_two=dict((e.get("name"), e) for e in root_two.findall("data"))

for eo in root_one.findall("data"):
    name=eo.get("name")
    tail=eo.tail
    eo.clear()
    eo.tail=tail
    en=data_two[name]
    for k,v in en.items():
        eo.set(k,v)
    eo.extend(en.findall("*"))
    eo.text=en.text

tree_one.write("one.xml")
</code></pre>

<p>If your files do not fit in memory you can still use <a href="https://docs.python.org/3/library/xml.dom.pulldom.html#module-xml.dom.pulldom" rel="nofollow">xml.dom.pulldom</a> as long as single <code>data</code> entries do fit.</p>

python xml