我已将此游戏数据以xml格式编写。
<?xml version="1.0" encoding="ISO-8859-1"?>
<log xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://mirror.server.eu/descr.xsd">
<version>0.1</version>
<info>
<timestamp>2018-09-23 16:09:23 CEST</timestamp>
<hostname>server.eu</hostname>
</info>
<events>
<event>
<pickup>
<time>1.506636</time>
<item>item_spikes</item>
<player>player1</player>
<value>50</value>
</pickup>
</event>
<event>
<damage>
<time>1.926975</time>
<attacker>player1</attacker>
<target>player2</target>
<type>sg</type>
<quad>0</quad>
<splash>0</splash>
<value>24</value>
<armor>0</armor>
</damage>
</event>
<event>
<death>
<time>4.862534</time>
<attacker>player2</attacker>
<target>player1</target>
<type>lg_beam</type>
<quad>0</quad>
<armorleft>0</armorleft>
<killheight>0</killheight>
<lifetime>4.862534</lifetime>
</death>
</event>
</events>
</log>
我需要解析它并清除所有称为“死亡”的事件。然后,我需要访问“死亡”部分中的每个元素。你能帮我吗?
答案 0 :(得分:1)
假设事件只能包含一个称为death的标签,您可以轻松地做到这一点:
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='your_game_events.xml')
for event in tree.iter(tag = 'death'):
for child in event:
print "%s: %s" % (child.tag, child.text)