无法使用python elementtree找到特定的节点/元素

时间:2017-07-11 20:22:16

标签: python xml python-2.7 elementtree

我正在尝试解析下面的XML文档。我只需要从文档中获取一个节点。我需要获取serviceProfile文本。我在这里把头撞到桌子上......我是Python的新手。

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getUserResponse
            xmlns:ns="http://www.cisco.com/AXL/API/11.5">
            <return>
                <user uuid="{blbhbl-bhblb-kbhb}">
                    <firstName>fname</firstName>
                    <displayName>fname lname</displayName>
                    <middleName/>
                    <lastName>lname</lastName>
                    <userid>wooty</userid>
                    <password/>
                    <pin/>
                    <mailid>wooty@woot.com</mailid>
                    <department/>
                    <manager/>
                    <userLocale />
                    <associatedDevices/>
                    <primaryExtension/>
                    <associatedPc/>
                    <enableCti>false</enableCti>
                    <digestCredentials/>
                    <phoneProfiles/>
                    <defaultProfile/>
                    <presenceGroupName uuid="{sdsds-sdsds-sdsdsd-sdsdsd-sdsd}">Standard Presence group</presenceGroupName>
                    <subscribeCallingSearchSpaceName/>
                    <enableMobility>false</enableMobility>
                    <enableMobileVoiceAccess>false</enableMobileVoiceAccess>
                    <maxDeskPickupWaitTime>10000</maxDeskPickupWaitTime>
                    <remoteDestinationLimit>4</remoteDestinationLimit>
                    <associatedRemoteDestinationProfiles/>
                    <associatedTodAccess/>
                    <status>1</status>
                    <enableEmcc>false</enableEmcc>
                    <associatedCapfProfiles/>
                    <ctiControlledDeviceProfiles/>
                    <patternPrecedence />
                    <numericUserId />
                    <mlppPassword />
                    <customUserFields/>
                    <homeCluster>true</homeCluster>
                    <imAndPresenceEnable>true</imAndPresenceEnable>
                    <serviceProfile uuid="{dsdsdsd-sdsdsd-sdsd-sdsds-sdsds}">1 IM Presence Only</serviceProfile>
                    <lineAppearanceAssociationForPresences/>
                    <directoryUri>blah@wooty.com</directoryUri>
                    <telephoneNumber>555-555-5555</telephoneNumber>
                    <title/>
                    <mobileNumber/>
                    <homeNumber/>
                    <pagerNumber/>
                    <extensionsInfo/>
                    <selfService />
                    <userProfile/>
                    <calendarPresence>false</calendarPresence>
                    <ldapDirectoryName uuid="{sdsd-sdsdsd-sdsds-sdsds}">someinfo</ldapDirectoryName>
                    <userIdentity>blah@woot.com</userIdentity>
                    <nameDialing>blehWoot</nameDialing>
                    <ipccExtension/>
                    <convertUserAccount uuid="{sdsd-sdsdsd-sdsds-sdsds}">someinfo</convertUserAccount>
                    <enableUserToHostConferenceNow>false</enableUserToHostConferenceNow>
                    <attendeesAccessCode/>
                </user>
            </return>
        </ns:getUserResponse>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

根据@danielHaley的建议,我创建了以下代码来检索节点。

#read XML response and get service profile
tree = ET.ElementTree(ET.fromstring(response.content))
root = tree.getroot()

serviceprofile = root.find(".//serviceProfile").text 

工作得很好。非常感谢你的帮助。