import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
xpathobjects = tree.findall(".//BuildingNodeBase[name = 'Building name']")
我想要使用值BuildingNodeBase
的子标记名称来提取Building name
。
但是获得:
SyntaxError(“invalid predicate”)
答案 0 :(得分:2)
The XPath support in ElementTree is limited,但支持您的表达类型。只需要删除=
:
.//BuildingNodeBase[name='Building name']
答案 1 :(得分:0)
我使用lxml
但我想你可以采用它来供你使用:
from lxml import etree
tree = etree.parse('test.xml')
xpathobjects = tree.xpath(".//BuildingNodeBase[@name = 'Building name']")