编辑:样本文件的更新答案。
我假设你想搜索每个资产的某些标签。如果是这样,以下内容对我有用:
import lxml.objectify
# Parse the file.
tree = lxml.objectify.parse('sample.xml')
root = tree.getroot()
# Which elements to find.
to_find = set(['presence/faction', 'presence/value', 'fake'])
# Go through each asset in the document.
for asset in root.findall('asset'):
# Check for each element.
for name in to_find:
node = asset.find(name)
if node is not None:
print 'Found %s, its value is %s' % (name, node)
else:
print 'Unable to find %s' % name输出是:
Found presence/value, its value is -1000.0
Found presence/faction, its value is Dvaered
Unable to find fake
Found presence/value, its value is 100.0
Found presence/faction, its value is Empire
Unable to find fake