register_namespace()是ElementTree
模块中
包含的功能。
它 不 包含在
ElementTree课程中…
旁白:由于这样做有时会造成混乱,因此通常不建议对模块和类使用相同的名称。但是,我们不会通过重命名广泛使用的模块来破坏生产代码,是吗?
您只需要更改代码:
#!/usr/bin/pythonimport xml.etree.ElementTree as ET # import entire module; use alias for clarityimport sys, os# note that this is the *module*'s `register_namespace()` functionET.register_namespace("android", "http://schemas.android.com/apk/res/android")tree = ET.ElementTree() # instantiate an object of *class* `ElementTree`tree.parse("AndroidManifest.xml")root = tree.getroot()root.attrib["{http://schemas.android.com/apk/res/android}versionCode"] = "3"ET.dump(tree) # we use the *module*'s `dump()` function


