Увеличение версии в файле xml с использованием скрипта python - PullRequest
1 голос
/ 06 января 2020
    /* 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:*" />

Как только скрипт выполняется, номер версии увеличивается на 1, чего я пытался достичь. Но в файл добавляется тег ns0, а заголовок XML info-тег удаляется [].

Пожалуйста, дайте мне знать, что я сделал неправильно.

1 Ответ

0 голосов
/ 06 января 2020

Ваш скрипт немного изменен:

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()

Результат:

<?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>

Одно из объявлений пространства имен удалено, поскольку оно не использовалось в теле XML.

Если вы хотите сохранить пространства имен, используйте библиотеку l xml. В этом случае ваш код будет выглядеть следующим образом (уведомление № 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()

В этом случае вывод:

<?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>
...