- 调入需要使用到的库 --- 读取根节点
import xml.etree.ElementTree as ET
tree = ET.ElementTree(file="AndroidManifest.xml")
root = tree.getroot()
- 获取某节点的属性(比如根节点)
tree = ET.ElementTree(file="AndroidManifest.xml")
root = tree.getroot()
//获取所有属性 -返回一个字典
dict = root.attrib
//获取自定属性的值 - 返回字符串
package = root.attrib["package"]
- 获取某节点的标签tag(比如根节点)
tree = ET.ElementTree(file="AndroidManifest.xml")
root = tree.getroot()
//获取自定属性的值 - 返回字符串
tag = root.tag
4.获取某节点的所有子节点
tree = ET.ElementTree(file="AndroidManifest.xml")
root = tree.getroot()
for child in root:
print(child.tag)
5.获取某节点的值
tree = ET.ElementTree(file="AndroidManifest.xml")
root = tree.getroot()
for child in root:
if child.tag == "qqq":
print(child.text)
6.修改完成后 将旧的根节点形成一个xml tree ,再将新的tree写入文件
newtree = ET.ElementTree(root)
newtree.write("AndroidManifest.xml", encoding="utf-8", xml_declaration=True)
网友评论