Finding values in XML document using Python -


i have following code tries values xml document:

from xml.dom import minidom xml = """<soccerfeed timestamp="20130328t152947+0000">            <soccerdocument uid="f131897" type="result" />              <competition uid="c87">              <matchdata>                <matchinfo timestamp="20070812t144737+0100" weather="windy"period="fulltime" matchtype="regular" />                <matchofficial uid="o11068"/>                <stat type="match_time">91</stat>                <teamdata teamref="t810" side="home" score="4" />                <teamdata teamref="t2012" side="away" score="1" />              </matchdata>              <team uid="t810" />              <team uid="t2012" />              <venue uid="v2158" />            </soccerdocument>          </soccerfeed>"""  xmldoc = minidom.parsestring(xml) soccerfeed = xmldoc.getelementsbytagname("soccerfeed")[0] soccerdocument = soccerfeed.getelementsbytagname("soccerdocument")[0]  #match data matchdata = soccerdocument.getelementsbytagname("matchdata")[0] matchinfo = matchdata.getelementsbytagname("matchinfo")[0] goal = matchdata.getelementsbytagnamens("side", "score") 

the goal being set [], score value, 4.

it looks searching wrong xml node. check following line:

goal = matchdata.getelementsbytagnamens("side", "score") 

you looking following:

goal = matchdata.getelementsbytagname("teamdata")[0].getattribute("score") 

note: document.getelementsbytagname, document.getelementsbytagnamens, element.getelementsbytagname, element.getelementsbytagnamens return list of nodes, not scalar value.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -