Reputation: 1357
I'm trying to search an XML file. I need to
I can get to the localteam element but I'm not sure how to then search for the awayteam and also go 'up' a level to get the id... I've tried using child.iter() and child.find('awayteam'), but neither has worked..
Thus far I have..
for child in root.iter('localteam'):
gs_name = child.get('name')
if gs_name == 'Team A':
print child.tag, child.attrib
for step_child in child.iter():
print step_child.tag, step_child.attrib
gs_name = child.get('name')
XML
<scores sport="sports">
<category name="NAME" id="1419" file_group="USA">
<match date="21.07.2013" time="04:00" status="Finished" id="56456456">
<localteam name="Team A" />
<random name="yyy" />
<awayteam name="Team B" />
<random name="xxx" />
</match>
Upvotes: 1
Views: 81
Reputation: 940
Search for match
elements instead:
for match in root.iter("match"):
if (match.find("localteam").get("name") == "Team A" and
match.find("awayteam").get("name") == "Team B"):
print match.get("id")
break
The above will raise AttributeErrors if the find
calls don't find anything, so you may want to add some additional checks or error handling; e.g.
for match in root.iter("match"):
localteam = match.find("localteam")
awayteam = match.find("awayteam")
if localteam is not None and awayteam is not None and ...
Upvotes: 2