官方文档:https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
解析xml文档:将xml文档解析,获取文档中的内容
采用xml.tree.ElementTree模块的parse函数
from xml.etree.ElementTree import parse
path = r'E:\settings.xml'
f = open(path)
tree= parse(path) #创建一个xml对象
root = tree.getroot() #获得结构树根节点
print('输出根节点的标签:%s'%root.tag) #.tag获取标签值
print('输出根节点的属性:%s'%root.attrib) #.attrib获取属性值
#获取root下的子节点
for child in root:
print('子节点标签:%s,子节点属性%s:'%(child.tag,child.attrib))#只能找出root下的第一级子节点
print('root下第一个子节点下的第二个子节点的内容%s:'%root[0][1].text)
print('输出所以结点内容%s:'