Parsing XML file having multiple tag , attributes and value

I am trying to parse an xml file having below schema:

<game gameId="cricket">
    <Period duration="1year" endTime="2017-12-31"/>
    <repPeriod duration="1year"/>
    <player p="1">sachin</player>
    <player p="2">rahul</player>
    <player p="3">saurav</player>
    <player p="4">kapil</player>
    <player p="5">sanjay</player>
    <player p="6">kartik</player>
    <player p="7">michel</player>
    <player p="8">rickey</player>
    <ranking period="2016">
        <r p="1">3</r>
    </ranking>
    <ranking period="DEFAULT">
        <r p="2">4</r>
        <r p="3">16</r>
        <r p="4">16</r>
        <r p="5">6</r>
        <r p="6">3</r>
        <r p="7">7</r>
        <r p="8">7</r>
    </ranking>
</game>

i am not able to find how player with attribute p=”1″ can be mapped to corresponding ranking value

my desired output is :

Player : ranking

sachin : 3

rahul : 4

My code so far:

from xml.dom import minidom

doc = minidom.parse('report.xml')
node = doc.documentElement
gameinfo = doc.getElementsByTagName("game")

counterlist = ['cricket','football']
for gameid in gameinfo:
    for counter in counterlist:
        if gameid.getAttribute('game') == counter:
            itemlist = counter.getElementsByTagName("player")
            i = len(itemlist)
            j = 1
            while j<=i:
                for itemnumber in itemlist:
                    if itemnumber.getAttribute('p') == j:
                        Playername = gameid.getElementsByTagName("player")[j].childNodes[0].data
                        rankid = gameid.getElementsByTagName("r")[j].childNodes[0].data
                        print (playername : rankid)

                j = j+1

Answer

The easiest way to do this would be to make a dictionary that stores the player name and ID (i.e. store <player p="1">sachin</player> as { '1': 'sachin' }, and then iterate through the rankings and use the stored player name data to populate your output.

# collect player name and ID
pdic = {}
playerlist = doc.getElementsByTagName("player")
for item in playerlist:
    pdic[ item.getAttribute('p') ] = item.childNodes[0].data

# get all the rankings
for r in doc.getElementsByTagName('r'):
    # get attribute `p` and find it in our dictionary
    if r.getAttribute('p') in pdic:
        print( pdic[r.getAttribute('p')] + ": " + r.childNodes[0].data )

Output:

sachin: 3
rahul: 4
saurav: 16
kapil: 16
sanjay: 6
kartik: 3
michel: 7
rickey: 7

Leave a Reply

Your email address will not be published. Required fields are marked *