0

I have following code that tries to get values from 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 is being set to [], but I would like to get the score value, which is 4.

artdanil
  • 4,952
  • 2
  • 32
  • 49
  • 2
    Are you even able to parse the XML you presented? `minidom.parse()` takes a required parameter of file. Clearly the string `xml` is not valid representation of that file. Please try to provide a working sample to get a better help in your question. – artdanil Jul 31 '13 at 19:41

1 Answers1

0

It looks like you are searching for the wrong XML node. Check following line:

Goal = MatchData.getElementsByTagNameNS("Side", "Score")

You probably are looking for following:

Goal = MatchData.getElementsByTagName("TeamData")[0].getAttribute("Score")

NOTE: Document.getElementsByTagName, Document.getElementsByTagNameNS, Element.getElementsByTagName, Element.getElementsByTagNameNS return a list of nodes, not just a scalar value.

artdanil
  • 4,952
  • 2
  • 32
  • 49
  • (u'Home Team: 1. FSV Mainz 05', u'4', 'vs', u'1', u'Away Team: TuS Koblenz') I got this result! Thanks! Any ideas on how can I get rid of the u at the beginning? – Javier Galindo Aug 01 '13 at 04:14
  • Try using search or Google. The first answer in Google - [Python string prints as \[u'String'\]](http://stackoverflow.com/q/599625/214178) – artdanil Aug 01 '13 at 04:40