vendredi 17 février 2017

python 2.7 - check if element is xml object and get element with xpath

I have a list like:

game[obj1,obj2,false,obj3]

The objects in the list are xml objects where i need a note from.

xml example objects/notes:

<game>
    <name>AAA</name>
    <lvl>string1</lvl>
</game>
<game>
     <name>BBB</name>
     <lvl>string2</lvl>
</game>
<game>
    <name>XXX</name>
</game>
<game>
     <name>YYY</name>
     <lvl>string4</lvl>
</game>

my code:

searchforxpath= ".//lvl"
game=[obj1,obj2,false,obj3]
lyst=[]

for elem in game:
    if elem != False:
        subelem= elem.find(searchforxpath)
        if subelem!= None:
            lyst.append(subelem.text)           
        else:
            lyst.append(False)
    else:
        pass

print lyst

I expect lyst[string1,string2,false,string4]

But i get lyst[false,false,false,false]

Somehow the line if subelem!= None: is passed.

What is wrong with my code? Can't see it. If it helps, i can change the value False from the input list game to something else like None.

Any help would be great!

Aucun commentaire:

Enregistrer un commentaire