如何用Python高效读取XML文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计184个文字,预计阅读时间需要1分钟。
pythonimport xml.etree.ElementTree as ET
with open(web.xml, r) as f: et=ET.parse(f) root=et.getroot() print(root.tag)
f=open("web.xml")
et=parse(f)
root=et.getroot()
print(root.tag)
# print(root.attrib)
# print(root.text)
# print(list(root.iter()))#可以找到所有的节点#
# print(root.find("param-value"))#只可以找到它自己这个子层级下面的元素
print(root.findall(".//listener-class"))#查找子节点叫这个名字的所有元素
print(root.findall("listener/*"))#选择子节点下的所有元素
print(root.findall(".//listener-class/.."))#选择某个节点的所有夫元素
print(root.findall(".//listener[@name]"))#选择某个节点包含某个属性的所有元素
print(root.findall(".//listener-class[@name]"))
本文共计184个文字,预计阅读时间需要1分钟。
pythonimport xml.etree.ElementTree as ET
with open(web.xml, r) as f: et=ET.parse(f) root=et.getroot() print(root.tag)
f=open("web.xml")
et=parse(f)
root=et.getroot()
print(root.tag)
# print(root.attrib)
# print(root.text)
# print(list(root.iter()))#可以找到所有的节点#
# print(root.find("param-value"))#只可以找到它自己这个子层级下面的元素
print(root.findall(".//listener-class"))#查找子节点叫这个名字的所有元素
print(root.findall("listener/*"))#选择子节点下的所有元素
print(root.findall(".//listener-class/.."))#选择某个节点的所有夫元素
print(root.findall(".//listener[@name]"))#选择某个节点包含某个属性的所有元素
print(root.findall(".//listener-class[@name]"))

