简体   繁体   中英

How to get values from this XML?

I want to parse xml like this:

<?xml version="1.0" ?>
<matches>
    <round_1>
        <match_1>
            <home_team>team_5</home_team>
            <away_team>team_13</away_team>
            <home_goals_time>None</home_goals_time>
            <away_goals_time>24;37</away_goals_time>
            <home_age_average>27.4</home_age_average>
            <away_age_average>28.3</away_age_average>
            <score>0:2</score>
            <ball_possession>46:54</ball_possession>
            <shots>8:19</shots>
            <shots_on_target>2:6</shots_on_target>
            <shots_off_target>5:10</shots_off_target>
            <blocked_shots>1:3</blocked_shots>
            <corner_kicks>3:4</corner_kicks>
            <fouls>10:12</fouls>
            <offsides>0:0</offsides>
        </match_1>
    </round_1>
</matches>

I use standard library - xml but I can't get values from inner tags. That's my exemplary code:

import xml.etree.ElementTree as et
TEAMS_STREAM = "data/stats1.xml"

tree = et.parse(TEAMS_STREAM)
root = tree.getroot()

for elem in root.iter('home_goals_time'):
    print(elem.attrib)

It should work but it's not. I was trying to find issue in xml structure but I coludn't find it. I always got empty dict. Can you tell me what's wrong?

You are calling .attrib on the element, but there are no attributes for those elements. If you want to print the inner text of the element, use .text instead of .attrib

for elem in root.iter('home_goals_time'):
    print(elem.text)

The reason you're having issues is that you need to parse through the xml level by level. Using findall , I was able to get the value inside <home_goals_time> .

for i in root.findall('.//home_goals_time'):
     print (i.text)
None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM