python解析svg,XML解析之ElementTree
元素查找方法
一、match为tag;不含子节点,返回第一个/所有匹配的Element;可查找特定属性attrib[@id="title1"]
find(match)
findall(match)
二、含子节点,返回所有匹配的,不查找特定属性。。以当前元素为根节点 创建树迭代器,如果tag不为None,则以tag进行过滤
iter(tag=None)
三、不含子节点,返回所有匹配的。可查找属性。
iterfind(match)
### 导入依赖包
import xml.etree.ElementTree as etree
# 打开文件
tree = etree.ElementTree(file=r'/home/260190/PycharmProjects/auto_svgtopdf/test.svg') # 保证每次操作均为原始model文件
root = tree.getroot()
# find , findall
# 在test.svg中添加以下内容:
#
# nihao
#
# findall
# 所有tag,输出列表,for循环列出,不包含嵌套
elems = root.findall('{http://www.w3.org/2000/svg}g[@id="switch"]')
print(elems)
for elem in ele