from lxml import etree
import os
## 解析xml
E = etree.parse(xml) ## xml为xml文件路径
## 获取xml对应的节点信息
xstr = E.xpath('//function') ## 查找function对应的节点
## 判断节点是否存在
len(xstr) ## 节点是否存在 len(xstr) == 0 则不存在
## 访问第0节点的属性"code"
xstr[0].attrib["code"]
## 判断节点属性"type"是否存在
xstr[0].attrib.get("type") ## xstr[0].attrib.get("type") is None 则不存在
关于Python解析xml的一些用法
于 2023-03-31 16:31:05 首次发布