1

If I have an XML file such as below, how would I be able to change the version from 50 to 51?

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

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.

Thanks!

4
  • Do you know if there will always be just 1 instance of unturnedVersion? If there are multiple, do you want to do this to all of them, or just the first, last, nth, etc.? Commented Aug 26, 2016 at 15:41
  • There will just be one instance. Commented Aug 26, 2016 at 15:41
  • do you want to go xml or nasty hack-like (text replace)? Commented Aug 26, 2016 at 15:41
  • I'd prefer XML, if I couldn't get it to work, I was just going to make a separate file and just leave that version in it so I could just write over it... haha Commented Aug 26, 2016 at 15:42

1 Answer 1

5

Use xml.etree.ElementTree. Locate the element via, for example, find(), update the version attribute through the .attrib dictionary of an element:

import xml.etree.ElementTree as ET

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

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

print(ET.tostring(root))

Prints:

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

Note that, if you want to increment the existing version, use:

unturned_version.attrib["version"] = str(int(unturned_version.attrib["version"]) + 1)

And, if you read an XML from file, use ET.parse():

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Does this work: root = ET.parse(os.path.join(MANAGER_FOLDER, "config_RocketUpdater.xml")) ... I have to read the input from an XML file first. I don't get any errors when I change it to that, but the program doesn't change the value either.
@AubtinSamai updated with a sample code to read XML from file, works for me.
Thanks, it works! Just had to add write so it would print to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.