我正在AndroidManifest.xml用 Python 的ElementTree. 我需要将android命名空间注册为,http://schemas.android.com/apk/res/android否则 ElementTree 会将其替换为ns0. 这是不直观的,但现在它可以工作了。
在访问节点的属性时,我希望能够简单地指定例如。elem.attrib["android:versionCode"]. 但它不起作用,因为 ElementTree 希望我像这样使用它:
ET.register_namespace("android", "http://schemas.android.com/apk/res/android")
tree = ET.ElementTree()
tree.parse("AndroidManifest.xml")
root = tree.getroot()
root.attrib["{http://schemas.android.com/apk/res/android}versionCode"] = "3"
即使在文件中它曾经是并且将会是android:versionCode.
由于这是违反直觉的,有什么方法可以root.attrib["android:versionCode"]代替吗?