Change an XML Value in Python
<p>If I have an XML file such as below, how would I be able to change the version from 50 to 51?</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;config&gt;
    &lt;updateCheck seconds="2" /&gt;
    &lt;unturnedVersion version="50" /&gt;
    &lt;unturnedFolder recoveryBundlesAfterUpdates="false" /&gt;
    &lt;rocket useRocket="true" apikey=""/&gt;
    &lt;steam username="" password="" /&gt;
    &lt;steamUpdates validate="true" /&gt;
    &lt;servers rconEnabled="false"&gt;
        &lt;server name="server1" rconPort="27013" rconPassword="pass" /&gt;
        &lt;server name="server2" rconPort="27014" rconPassword="pass" /&gt;
    &lt;/servers&gt;
    &lt;notifyBefore seconds="60" /&gt;
&lt;/config&gt;
</code></pre>

<p>I've tried multiple methods to do it, and some don't do anything or it just creates a new version of the unturnedVersion with 51 at the bottom of the code. I want to simply change 50 to 51 or any other value I set.</p>

<p>Thanks!</p>

<p>Use <a href="https://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow"><code>xml.etree.ElementTree</code></a>. Locate the element via, for example, <code>find()</code>, update the <code>version</code> attribute through the <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.attrib" rel="nofollow"><code>.attrib</code> dictionary</a> of an element:</p>

<pre><code>import xml.etree.ElementTree as ET

data = """&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;config&gt;
    &lt;updateCheck seconds="2" /&gt;
    &lt;unturnedVersion version="50" /&gt;
    &lt;unturnedFolder recoveryBundlesAfterUpdates="false" /&gt;
    &lt;rocket useRocket="true" apikey=""/&gt;
    &lt;steam username="" password="" /&gt;
    &lt;steamUpdates validate="true" /&gt;
    &lt;servers rconEnabled="false"&gt;
        &lt;server name="server1" rconPort="27013" rconPassword="pass" /&gt;
        &lt;server name="server2" rconPort="27014" rconPassword="pass" /&gt;
    &lt;/servers&gt;
    &lt;notifyBefore seconds="60" /&gt;
&lt;/config&gt;"""

root = ET.fromstring(data)
unturned_version = root.find("unturnedVersion")
unturned_version.attrib["version"] = "51"

print(ET.tostring(root))
</code></pre>

<p>Prints:</p>

<pre><code>&lt;config&gt;
    &lt;updateCheck seconds="2" /&gt;
    &lt;unturnedVersion version="51" /&gt;
    &lt;unturnedFolder recoveryBundlesAfterUpdates="false" /&gt;
    &lt;rocket apikey="" useRocket="true" /&gt;
    &lt;steam password="" username="" /&gt;
    &lt;steamUpdates validate="true" /&gt;
    &lt;servers rconEnabled="false"&gt;
        &lt;server name="server1" rconPassword="pass" rconPort="27013" /&gt;
        &lt;server name="server2" rconPassword="pass" rconPort="27014" /&gt;
    &lt;/servers&gt;
    &lt;notifyBefore seconds="60" /&gt;
&lt;/config&gt;
</code></pre>

<p>Note that, if you want to increment the existing version, use:</p>

<pre><code>unturned_version.attrib["version"] = str(int(unturned_version.attrib["version"]) + 1)
</code></pre>

<hr>

<p>And, if you read an XML from file, use <code>ET.parse()</code>:</p>

<pre><code>import xml.etree.ElementTree as ET


tree = ET.parse("input.xml")
root = tree.getroot()
unturned_version = root.find("unturnedVersion")
unturned_version.attrib["version"] = str(int(unturned_version.attrib["version"]) + 1)

print(ET.tostring(root))
</code></pre>

python xml