我有以下代码尝试从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
。
答案 0 :(得分:0)
看起来您正在搜索错误的XML节点。检查以下行:
Goal = MatchData.getElementsByTagNameNS("Side", "Score")
您可能正在寻找以下内容:
Goal = MatchData.getElementsByTagName("TeamData")[0].getAttribute("Score")
注意: Document.getElementsByTagName,Document.getElementsByTagNameNS,Element.getElementsByTagName,Element.getElementsByTagNameNS返回节点列表,而不仅仅是标量值。