Android与Python之批量修改AndroidManifest.xml文件

1,598 阅读1分钟

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