在使用Python脚本中增加XML文件中的版本。

huangapple go评论90阅读模式
英文:

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 = list(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" />
</widget>

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

请注意,代码中的注释也已被翻译。如果需要进一步的帮助或翻译其他内容,请告诉我。

英文:
    /* Python Script */
import xml.etree.ElementTree as ET
tree = ET.parse(&#39;config.xml&#39;)
root = tree.getroot()
updateData = open(&#39;config.xml&#39;,&#39;w+&#39;)
print(&#39;Root Data is &#39;,root.tag)
print(&#39;Root Attribute &#39;,root.attrib)
old_version = root.attrib.values()[0]
print(&#39;Old_Version is &#39;,old_version)
def increment_ver(old_version):
old_version = old_version.split(&#39;.&#39;)
old_version[2] = str(int(old_version[2]) + 1)
print(&#39;Old_Version 2 &#39;,old_version[2])
return &#39;.&#39;.join(old_version)	
new_Version = increment_ver(old_version);
print(&#39;New_version :&#39;,new_Version,root.attrib[&#39;version&#39;])
root.attrib[&#39;version&#39;] = new_Version
print(root.attrib)
tree.write(updateData)
updateData.close()
/* Original Config xml file */
&lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&gt;
&lt;widget id=&quot;io.ionic.starter&quot; version=&quot;0.0.1&quot; xmlns=&quot;http://www.w3.org/ns/widgets&quot; xmlns:cdv=&quot;http://cordova.apache.org/ns/1.0&quot;&gt;
&lt;name&gt;aman&lt;/name&gt;
&lt;description&gt;An awesome Ionic/Cordova app.&lt;/description&gt;
&lt;author email=&quot;hi@ionicframework.com&quot; href=&quot;http://ionicframework.com/&quot;&gt;Ionic Framework Team&lt;/author&gt;
&lt;content src=&quot;index.html&quot; /&gt;
&lt;access origin=&quot;*&quot; /&gt;
&lt;allow-intent href=&quot;http://*/*&quot; /&gt;
&lt;allow-intent href=&quot;https://*/*&quot; /&gt;
&lt;allow-intent href=&quot;tel:*&quot; /&gt;
&lt;allow-intent href=&quot;sms:*&quot; /&gt;
&lt;allow-intent href=&quot;mailto:*&quot; /&gt;
&lt;allow-intent href=&quot;geo:*&quot; /&gt;
&lt;preference name=&quot;ScrollEnabled&quot; value=&quot;false&quot; /&gt;
/* New Config.xml file */
&lt;ns0:widget xmlns:ns0=&quot;http://www.w3.org/ns/widgets&quot; xmlns:ns1=&quot;http://schemas.android.com/apk/res/android&quot; id=&quot;io.ionic.starter&quot; version=&quot;0.0.2&quot;&gt;
&lt;ns0:name&gt;aman&lt;/ns0:name&gt;
&lt;ns0:description&gt;An awesome Ionic/Cordova app.&lt;/ns0:description&gt;
&lt;ns0:author email=&quot;hi@ionicframework.com&quot; href=&quot;http://ionicframework.com/&quot;&gt;Ionic Framework Team&lt;/ns0:author&gt;
&lt;ns0:content src=&quot;index.html&quot; /&gt;
&lt;ns0:access origin=&quot;*&quot; /&gt;
&lt;ns0:allow-intent href=&quot;http://*/*&quot; /&gt;
&lt;ns0:allow-intent href=&quot;https://*/*&quot; /&gt;
&lt;ns0:allow-intent href=&quot;tel:*&quot; /&gt;
&lt;ns0:allow-intent href=&quot;sms:*&quot; /&gt;
&lt;ns0:allow-intent href=&quot;mailto:*&quot; /&gt;

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 [<?xml version='1.0' encoding='utf-8'?>].

Please let me know what i have done wrong.

答案1

得分: 0

你的脚本稍作修改:

&lt;!-- language: python --&gt;
    
import xml.etree.ElementTree as ET

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

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

# (...) 代码的这部分没有变化。

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

结果:

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

其中一个命名空间声明已被删除,因为它在XML主体中未使用。

如果要保留命名空间,请使用lxml库。在这种情况下,你的代码看起来像这样(请注意没有ET.register_namespace):

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

# (...) 代码的这部分没有变化。

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

在这种情况下的输出是:

&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;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"&gt;
    &lt;name&gt;aman&lt;/name&gt;
    &lt;description&gt;An awesome Ionic/Cordova app.&lt;/description&gt;
    &lt;author email="hi@ionicframework.com" href="http://ionicframework.com/"&gt;Ionic Framework Team&lt;/author&gt;
    &lt;content src="index.html"/&gt;
    &lt;access origin="*"/&gt;
    &lt;allow-intent href="http://*/*"/&gt;
    &lt;allow-intent href="https://*/*"/&gt;
    &lt;allow-intent href="tel:*"/&gt;
    &lt;allow-intent href="sms:*"/&gt;
    &lt;allow-intent href="mailto:*"/&gt;
    &lt;allow-intent href="geo:*"/&gt;
    &lt;preference name="ScrollEnabled" value="false"/&gt;
&lt;/widget&gt;
英文:

Your script slightly modified:

<!-- language: python -->

import xml.etree.ElementTree as ET
ET.register_namespace(&#39;&#39;, &#39;http://www.w3.org/ns/widgets&#39;)
tree = ET.parse(&#39;config.xml&#39;)
# (...) no changes in this part of code.
tree.write(f, xml_declaration=True, encoding=&quot;utf-8&quot;)
updateData.close()

The result:

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

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(&#39;config.xml&#39;)
root = tree.getroot()
updateData = open(&#39;config.xml&#39;,&#39;w+&#39;)
# (...) no changes in this part of code.
tree.write(f, xml_declaration=True, encoding=&quot;utf-8&quot;)
updateData.close()

In this case the output:

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

huangapple
  • 本文由 发表于 2020年1月6日 20:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定