繁体   English   中英

使用python脚本增加xml文件中的版本

[英]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:*" />

一旦脚本被执行,版本号就会增加 1,这是我试图实现的。 但是,ns0 标记被添加到整个文件中,并且标题 XML 信息标记被删除 []。

请让我知道我做错了什么。

您的脚本略有修改:

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 正文中使用。

如果要保留命名空间,请使用lxml库。 在这种情况下,您的代码将如下所示(注意没有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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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