Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python脚本提高xml文件的版本 /*Python脚本*/ 将xml.etree.ElementTree作为ET导入 tree=ET.parse('config.xml') root=tree.getroot() updateData=open('config.xml','w+')) 打印('根数据为',Root.tag) 打印('Root属性',Root.attrib) 旧版本=root.attrib.values()[0] 打印(“旧版本为”,旧版本) def增量版本(旧版本): 旧版本=旧版本。拆分('.')) 旧版本[2]=str(int(旧版本[2])+1) 打印('旧版本2',旧版本[2]) 返回“.”。加入(旧版本) 新版本=增量版本(旧版本); 打印('New_version:',New_version,root.attrib['version'])) root.attrib['version']=新版本 打印(root.attrib) tree.write(updateData) updateData.close() /*原始配置xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组 /*新的Config.xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组_Python_Xml_Elementtree - Fatal编程技术网

使用python脚本提高xml文件的版本 /*Python脚本*/ 将xml.etree.ElementTree作为ET导入 tree=ET.parse('config.xml') root=tree.getroot() updateData=open('config.xml','w+')) 打印('根数据为',Root.tag) 打印('Root属性',Root.attrib) 旧版本=root.attrib.values()[0] 打印(“旧版本为”,旧版本) def增量版本(旧版本): 旧版本=旧版本。拆分('.')) 旧版本[2]=str(int(旧版本[2])+1) 打印('旧版本2',旧版本[2]) 返回“.”。加入(旧版本) 新版本=增量版本(旧版本); 打印('New_version:',New_version,root.attrib['version'])) root.attrib['version']=新版本 打印(root.attrib) tree.write(updateData) updateData.close() /*原始配置xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组 /*新的Config.xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组

使用python脚本提高xml文件的版本 /*Python脚本*/ 将xml.etree.ElementTree作为ET导入 tree=ET.parse('config.xml') root=tree.getroot() updateData=open('config.xml','w+')) 打印('根数据为',Root.tag) 打印('Root属性',Root.attrib) 旧版本=root.attrib.values()[0] 打印(“旧版本为”,旧版本) def增量版本(旧版本): 旧版本=旧版本。拆分('.')) 旧版本[2]=str(int(旧版本[2])+1) 打印('旧版本2',旧版本[2]) 返回“.”。加入(旧版本) 新版本=增量版本(旧版本); 打印('New_version:',New_version,root.attrib['version'])) root.attrib['version']=新版本 打印(root.attrib) tree.write(updateData) updateData.close() /*原始配置xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组 /*新的Config.xml文件*/ 阿曼 一款很棒的Ionic/Cordova应用程序。 离子框架小组,python,xml,elementtree,Python,Xml,Elementtree,一旦脚本被执行,版本号将增加1,这是我试图实现的。但是,整个文件中都添加了ns0标记,并且删除了头XML info标记[] 请让我知道我做错了什么。您的脚本稍有修改: 将xml.etree.ElementTree作为ET导入 ET.register_名称空间(“”,'http://www.w3.org/ns/widgets') tree=ET.parse('config.xml') #(…)这部分代码没有更改。 write(f,xml\u声明=True,encoding=“utf-8”) upd

一旦脚本被执行,版本号将增加1,这是我试图实现的。但是,整个文件中都添加了ns0标记,并且删除了头XML info标记[]


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

您的脚本稍有修改:

将xml.etree.ElementTree作为ET导入
ET.register_名称空间(“”,'http://www.w3.org/ns/widgets')
tree=ET.parse('config.xml')
#(…)这部分代码没有更改。
write(f,xml\u声明=True,encoding=“utf-8”)
updateData.close()
结果是:

    /* 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:*" />
在这种情况下,输出:

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

阿曼
一款很棒的Ionic/Cordova应用程序。
离子框架小组

您的脚本稍有修改:

将xml.etree.ElementTree作为ET导入
ET.register_名称空间(“”,'http://www.w3.org/ns/widgets')
tree=ET.parse('config.xml')
#(…)这部分代码没有更改。
write(f,xml\u声明=True,encoding=“utf-8”)
updateData.close()
结果是:

    /* 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:*" />
在这种情况下,输出:

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

阿曼
一款很棒的Ionic/Cordova应用程序。
离子框架小组
请参阅并参阅和