有没有办法获取XML标记的特定值?
<Country Rank="0">
<Name>xyz</Name>
<Place>abcd</IntValue>
</Country>
<Country Rank="1">
<Name>xyz1</Name>
<Place>abcd1</IntValue>
</Country>
<Country Rank="2">
<Name>xyz2</Name>
<Place>abcd2</IntValue>
</Country>
如何获得特定国家/地区的位置?
下面的代码会显示所有XML标记的文本。但我需要遍历特定的xpath。
from xml.etree import ElementTree
with open('file.xml', 'rt') as f:
tree = ElementTree.parse(f)
for node in tree.iter():
print node.tag
print node.text
答案 0 :(得分:1)
使用XPaths:
[place.text for country in e.findall(".//Country[@Rank='1']") for place in country.iter("Place")]
e
是您的树或元素根。 Xpath ".//Country[@Rank='1']"
中的排名值可用于更改所需的排名,因此您可以创建如下函数:
def get_places_by_rank(e, rank):
xpath = ".//Country[@Rank='{}']".format(rank)
return [place.text for country in e.findall(xpath) for place in country.iter("Place")]
并使用它:
>>> e=ET.fromstring("""
... <Countries>
... <Country Rank="0">
... <Name>xyz</Name>
... <Place>abcd</Place>
... </Country>
... <Country Rank="1">
... <Name>xyz1</Name>
... <Place>abcd1</Place>
... </Country>
... <Country Rank="2">
... <Name>xyz2</Name>
... <Place>abcd2</Place>
... </Country>
... </Countries>""")
>>>
>>> get_places_by_rank(e, 1)
['abcd1']
>>> get_places_by_rank(e, 2)
['abcd2']
>>> get_places_by_rank(e, 3)
[]
>>> get_places_by_rank(e, 0)
['abcd']