xml

read and modify a XML file

https://docs.python.org/2/library/xml.etree.elementtree.html

http://lxml.de/tutorial.html

http://effbot.org/zone/element.htm

Test-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>

    <viewOnlyMode>false</viewOnlyMode>

    <app1>

        <turnOff>false</turnOff>

        <enabled>false</enabled>

        <proxyEnabled>false</proxyEnabled>

        <Testing>

            <enabled>false</enabled>

        </Testing>

    </app1>

    <mobile>

        <turnOff>true</turnOff>

        <enabled>false</enabled>

        <proxyEnabled>false</proxyEnabled>

        <Testing>

            <enabled>false</enabled>

        </Testing>

    </mobile>

</config>

import xml.etree.ElementTree as ET

xmlfile = "c:/temp/Test-config.xml"

xmlfilenew = "c:/temp/Test-config2.xml"

tree = ET.parse(xmlfile)

print "App1 turnoff tag is currently set to: " + (tree.find(".app1/turnOff").text)

#tree.find(".app1/turnOff").text = "meh"

app1toggle = tree.find(".app1/turnOff")

app1toggle.text = "true"

print "Setting app1 turnoff tag to: " + (app1toggle.text)

#Save file

print "Saving changes to: " + xmlfilenew

tree.write(xmlfilenew)

Test-config2.xml

<config>

    <viewOnlyMode>false</viewOnlyMode>

    <app1>

        <turnOff>true</turnOff>

        <enabled>false</enabled>

        <proxyEnabled>false</proxyEnabled>

        <Testing>

            <enabled>false</enabled>

        </Testing>

    </app1>

    <mobile>

        <turnOff>true</turnOff>

        <enabled>false</enabled>

        <proxyEnabled>false</proxyEnabled>

        <Testing>

            <enabled>false</enabled>

        </Testing>

    </mobile>

</config>