python - Search child element in ET -


i'm trying search xml file. need

  • check if 'localteam' name matches query (e.g 'team a')
  • if match found check 'awayteam' name matches second condition (e.g 'team b'), elemt of below 'localteam' within same element
  • if 'awayteam' matches query id of parent (in 'match') , exit search.

i can localteam element i'm not sure how search awayteam , go 'up' level id... i've tried using child.iter() , child.find('awayteam'), neither has worked..

thus far have..

for child in root.iter('localteam'):     gs_name = child.get('name')     if gs_name == 'team a':         print child.tag, child.attrib         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> 

search match elements instead:

for match in root.iter("match"):     if (match.find("localteam").get("name") == "team a" ,         match.find("awayteam").get("name") == "team b"):         print match.get("id")         break 

the above raise attributeerrors if find calls don't find anything, may want add additional checks or error handling; e.g.

for match in root.iter("match"):     localteam = match.find("localteam")     awayteam = match.find("awayteam")     if localteam not none , awayteam not none , ... 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -