0

I am trying to parse the following SOAP response using xPath and ElementTree but with no luck. I am not sure if this is the best way to do this? if it's not happy to hear other suggestions.

I have have cut down the XML to keep it readable.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
  <ns1:SelectCmDeviceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://ccm.cisco.com/serviceability/soap/risport70/">
     <SelectCmDeviceResult xsi:type="ns1:SelectCmDeviceResult">
        <TotalDevicesFound xsi:type="xsd:unsignedInt">24</TotalDevicesFound>
        <CmNodes soapenc:arrayType="ns1:CmNode[4]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
           <item xsi:type="ns1:CmNode">
              <ReturnCode xsi:type="ns1:RisReturnCode">Ok</ReturnCode>
              <Name xsi:type="xsd:string">uk-cucm-pub</Name>
              <NoChange xsi:type="xsd:boolean">false</NoChange>
              <CmDevices soapenc:arrayType="ns1:CmDevice[12]" xsi:type="soapenc:Array">
                 <item xsi:type="ns1:CmDevice">
                    <Name xsi:type="xsd:string">SIP-Trunk-to-NO-Cluster</Name>
                    <DirNumber xsi:type="xsd:string" xsi:nil="true"/>
                    <Class xsi:type="ns1:DeviceClass">SIP Trunk</Class>
                    <Model xsi:type="xsd:unsignedInt">131</Model>
                    <Status xsi:type="ns1:CmDevRegStat">Registered</Status>

What I like to achieve is to be able to go through each item tag under the CmNodes (only one item tag is shown above but there are multiple) and then under the CmDevices/item if the Name tag text is equal to SIP-Trunk-to-NO-Cluster then get the text under the Status in this case this will be "Registered"

Thanks alexis

alexis
  • 1,022
  • 3
  • 16
  • 44
  • [Relevant](http://stackoverflow.com/questions/206154/what-soap-client-libraries-exist-for-python-and-where-is-the-documentation-for) and may go dhave mercy on your soul for having to use SOAP. – kylieCatt Jul 14 '15 at 17:50
  • @IanAuld thanks! will try out SUDS. – alexis Jul 14 '15 at 19:45

1 Answers1

0

OK found out the solution and though I would post it here in case anyone finds this useful.

I tried to use SUDS as suggested by @IanAuld (I think that would be the best solution) but run into SSL issues as the server I was pulling the WSDL from had a self sign cert.

The key for me to understanding name spaces was to use the following cmd and to use lxml

#print all the namespaces used in the xml doc
print root.nsmap

I then come up with the following code to get the info I needed

 #get all the namespace 
 sip_trunk_name_ele = root.findall(".//item[@xsi:type='ns1:CmNode']/CmDevices[@xsi:type='soapenc:Array']/item[@xsi:type='ns1:CmDevice']/Name[@xsi:type='xsd:string']", namespaces=root.nsmap)
    for sip_trunk_name in sip_trunk_name_ele:
        print(sip_trunk_name.tag,sip_trunk_name.text)
        if sip_trunk_name.text == "SIP-Trunk-to-NO-Cluster":
            print(sip_trunk_name.getparent().tag)
            sip_trunk = sip_trunk_name.getparent()
            return print sip_trunk.findtext('Status')

I am sure there is a better way to use lxml to get the information but this worked for me

alexis
  • 1,022
  • 3
  • 16
  • 44