xml - Parse a SOAP message with namespace in Python -
i trying parse following soap response using xpath , elementtree no luck. not sure if best way this? if it's not happy hear other suggestions.
i have have cut down xml keep 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 achieve able go through each item
tag under cmnodes
(only 1 item tag shown above there multiple) , under cmdevices/item
if name
tag text equal sip-trunk-to-no-cluster text under status
in case "registered"
thanks alexis
ok found out solution , though post here in case finds useful.
i tried use suds suggested @ianauld (i think best solution) run ssl issues server pulling wsdl had self sign cert.
the key me understanding name spaces use following cmd , use lxml
#print namespaces used in xml doc print root.nsmap
i come following code info needed
#get 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) 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 sure there better way use lxml
information worked me
Comments
Post a Comment