简体   繁体   中英

Increasing Version in xml file using python script

    /* Python Script */

    import xml.etree.ElementTree as ET
    tree = ET.parse('config.xml')
    root = tree.getroot()
    updateData = open('config.xml','w+')
    print('Root Data is ',root.tag)
    print('Root Attribute ',root.attrib)
    old_version = root.attrib.values()[0]
    print('Old_Version is ',old_version)
    def increment_ver(old_version):
        old_version = old_version.split('.')
        old_version[2] = str(int(old_version[2]) + 1)
        print('Old_Version 2 ',old_version[2])
        return '.'.join(old_version)    
    new_Version = increment_ver(old_version);
    print('New_version :',new_Version,root.attrib['version'])
    root.attrib['version'] = new_Version
    print(root.attrib)
    tree.write(updateData)
    updateData.close()

/* Original Config xml file */

    <?xml version='1.0' encoding='utf-8'?>
<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>aman</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="ScrollEnabled" value="false" />

/* New Config.xml file */

<ns0:widget xmlns:ns0="http://www.w3.org/ns/widgets" xmlns:ns1="http://schemas.android.com/apk/res/android" id="io.ionic.starter" version="0.0.2">
<ns0:name>aman</ns0:name>
<ns0:description>An awesome Ionic/Cordova app.</ns0:description>
<ns0:author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</ns0:author>
<ns0:content src="index.html" />
<ns0:access origin="*" />
<ns0:allow-intent href="http://*/*" />
<ns0:allow-intent href="https://*/*" />
<ns0:allow-intent href="tel:*" />
<ns0:allow-intent href="sms:*" />
<ns0:allow-intent href="mailto:*" />

Once the script gets executed the version number is increased by 1 which i was trying to achieve. But, ns0 tag is added throughout the file and the header XML info tag gets removed [].

Please let me know what i have done wrong.

Your script slightly modified:

import xml.etree.ElementTree as ET

ET.register_namespace('', 'http://www.w3.org/ns/widgets')

tree = ET.parse('config.xml')

# (...) no changes in this part of code.

tree.write(f, xml_declaration=True, encoding="utf-8")
updateData.close()

The result:

<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets" id="io.ionic.starter" version="0.0.2">
    <name>aman</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="ScrollEnabled" value="false" />
</widget>

One of the namespace declarations has been dropped because it was not used in the XML body.

If you want to preserve namespaces use lxml library. In this case, your code would look like this (notice no ET.register_namespace ):

import lxml.etree as ET

tree = ET.parse('config.xml')
root = tree.getroot()
updateData = open('config.xml','w+')

# (...) no changes in this part of code.

tree.write(f, xml_declaration=True, encoding="utf-8")
updateData.close()

In this case the output:

<?xml version='1.0' encoding='UTF-8'?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" id="io.ionic.starter" version="0.0.2">
    <name>aman</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html"/>
    <access origin="*"/>
    <allow-intent href="http://*/*"/>
    <allow-intent href="https://*/*"/>
    <allow-intent href="tel:*"/>
    <allow-intent href="sms:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="geo:*"/>
    <preference name="ScrollEnabled" value="false"/>
</widget>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM