Android與Python之批量修改AndroidManifest.xml檔案

別人家的開發發表於2018-05-03

Python2.7 a程式碼如下,只是個簡單的例子,實際開發自由發揮

# coding=utf-8
# 批量修改AndroidManifest

import xml.dom.minidom as xmldom

domTree = xmldom.parse('AndroidManifest.xml')
root = domTree.documentElement
application = root.getElementsByTagName('application')

for app in application:
    activity = app.getElementsByTagName('activity')

    for ac in activity:
        activityValue = ac.getAttribute('android:screenOrientation')

        if activityValue == "":
            ac.setAttribute('android:screenOrientation', 'portrait')
            print(ac.getAttribute('android:screenOrientation'))
        else:
            print '有'

# print(domTree.childNodes)

try:
    f = open("book_store.xml", "w")
    f.write(domTree.toprettyxml(indent="", newl="", encoding="utf-8"))
    f.close()

# with open('dom_write.xml', 'w', encoding='UTF-8') as fh:
#         # 4.writexml()第一個引數是目標檔案物件,第二個引數是根節點的縮排格式,第三個引數是其他子節點的縮排格式,
#         # 第四個引數制定了換行格式,第五個引數制定了xml內容的編碼。
#         domTree.writexml(fh, indent='', addindent='\t', newl='\n', encoding='UTF-8')
#         print('寫入xml OK!')
except Exception as err:
    print('錯誤資訊:{0}'.format(err))

複製程式碼

相關文章