Python XML属性返回'None'

时间:2018-03-24 18:35:57

标签: python python-2.7 xml-parsing

我试图用Python打印特定XML的两个不同属性。我的XML文件和我试图创建的Python脚本如下。我正确地获得了第一次打印而不是第二次打印。

XML:

<PreExecuteXml>
    <xmlElement>
        <action actionType="PlayGame" ugiToken="18638826-ecad-4e1a-a005-2306d3e68c99" uToken="2f53297e-5b88-55d7-b1dd-22fb9278ab29" msgId="jf5o4rtj447">
            <messages>
                <message type="Main">
                    <slotGame gsid="5752276184208378" mode="Fun" gi="52">
                        <request lin="10" linBet="1" denom="0.01" action="SPIN" bonLevel="" />
                    </slotGame>
                </message>
            </messages>
            <error HasError="false" />
            <creditBalance>0</creditBalance>
        </action>
    </xmlElement>
</PreExecuteXml>

Python脚本:

import xml.etree.ElementTree

xmlReq = xml.etree.ElementTree.fromstring(str(open("xml.txt", "rb").read()))
element = xmlReq.find("xmlElement/action")
action = element.attrib.get("actionType")

spinElement = xmlReq.find("xmlElement/action/messages/message/slotGame/request")
actionz = element.attrib.get("action")

print action #Prints -> "PlayGame" (correct)
print actionz #Prints -> None (Should print "SPIN")

结果:

C:\tests>python xmlz.py
PlayGame
None

0 个答案:

没有答案