[python]python脚本指定文件夹路径和版本号,批量修改xml文件版本号

问题描述

        python脚本实现,传入参数文件路径和版本号,批量修改xml文件版本号

XML文件

aaa.xml

<?xml version="1.0" ?><CONTENT date="2021-12-09" version="1.0">
	  <Generate>11111</Generate>
	  <Info>
	  	<Project>aaaaa</Project>
        <componentVersion>1.5</componentVersion>
	  	<Customer>CN</Customer>
	  </Info>
</CONTENT>

bbb.xml

<?xml version="1.0" ?><CONTENT date="2021-12-09" version="1.0">
	  <Generate>333333</Generate>
	  <Type>000</Type>
	  <Info>
	  	<Project>ccccc</Project>
        <componentVersion>1.5</componentVersion>
	  	<Customer>CN</Customer>
	  </Info>
</CONTENT>

 ccc.xml

<?xml version="1.0" ?><CONTENT date="2021-12-09" version="1.0">
	  <Generate>11111</Generate>
	  <Info>
	  	<Project1>bbbbb</Project1>
	  	<Project2>bbbbb</Project2>
	  	<Project3>bbbbb</Project3>
	  	<Project4>bbbbb</Project4>
        <componentVersion>1.5</componentVersion>
	  	<Customer>CN</Customer>
	  </Info>
</CONTENT>

代码实现

#coding=utf-8
import sys
import os
import xml.dom.minidom

path=sys.argv[1]
version=sys.argv[2]
print 'New version' +'='+version
print 'path='+path

def change_version(path,version):
  for root,dirs, files in os.walk(path):
      print(files) #get current files
      for sfile in files:
        if os.path.splitext(sfile)[1] == '.xml':  #get xml file
            #print sfile
            #打开xml文档
            dom = xml.dom.minidom.parse(sfile)
            #得到文档元素对象
            #root = dom.documentElement
            componementVersion=dom.getElementsByTagName('componentVersion')
            c1=componementVersion[0]
            print ">---------------------<"
            print sfile
            print "Old Version:"+c1.firstChild.data
            c1.firstChild.data=version
            print "New Version:"+c1.firstChild.data
            with open(os.path.join(path,sfile),'w') as fh:
                dom.writexml(fh)
  print ">----------end-----------<"

  return
change_version(path,version)

猜你喜欢

转载自blog.csdn.net/wgl307293845/article/details/121851010