0

我有以下代码试图从 XML 文档中获取值:

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")

Goal设置为[],但我想获得得分值,即4

4

1 回答 1

0

看起来您正在搜索错误的 XML 节点。检查以下行:

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

您可能正在寻找以下内容:

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

注意: Document.getElementsByTagNameDocument.getElementsByTagNameNSElement.getElementsByTagNameElement.getElementsByTagNameNS返回一个节点列表,而不仅仅是一个标量值。

于 2013-07-31T19:45:19.157 回答