Parsing soap result

darnold darnold992000 at yahoo.com
Wed Apr 17 13:55:13 EDT 2013


On Apr 17, 8:50 am, Ombongi Moraa Fe <moraa.lovetak... at gmail.com>
wrote:

> how do I use xml.etree.ElementTree to print the parameters address and
> deliveryStatus? Or is there a better python method?
>


I'm sure there are prettier ways to do this, but you can use XPath
syntax to find all of your ns1:result nodes and loop through them:

>>> import xml.etree.ElementTree as ET
>>> myXML = '''\
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:gere xmlns:ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/
local">
<ns1:result>
<address>254727</address>
<deliveryStatus>DeliveredToNetwork</deliveryStatus>
</ns1:result>
</ns1:gere>
</soapenv:Body>
</soapenv:Envelope>
'''
>>> myNamespaces=dict(ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local",soapenv="http://schemas.xmlsoap.org/soap/envelope/")
>>> root = ET.fromstring(myXML)
>>> for result in root.findall('.//ns1:result',namespaces=myNamespaces):
	address = result.find('address').text
	deliveryStatus = result.find('deliveryStatus').text
	print "address: %s, deliveryStatus: %s" % (address,deliveryStatus)


address: 254727, deliveryStatus: DeliveredToNetwork
>>>

HTH,
Don



More information about the Python-list mailing list