I have SOAP response:
<?xml version='1.0' encoding='utf-8'?><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><createsessionresponse soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"><createsessionreturn xsi:type="xsd:string">59C3F170141E9CF6F5BF98FB39B0237B</createsessionreturn></createsessionresponse></soapenv:body></soapenv:envelope>
objectify.dump returns this:
{http://schemas.xmlsoap.org/soap/envelope/}Envelope = None [ObjectifiedElement]
{http://schemas.xmlsoap.org/soap/envelope/}Body = None [ObjectifiedElement]
createSessionResponse = None [ObjectifiedElement]
* {http://schemas.xmlsoap.org/soap/envelope/}encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/'
createSessionReturn = '701C301EAA37A965B1B54A8EFD9ACD6F' [StringElement]
* xsi:type = 'xsd:string'
How can i access the value of createSessionReturn ?
createSessionResponse
is not using the http://schemas.xmlsoap.org/soap/envelope/
namespace
>>> import lxml.objectify
>>> doc = """<?xml version='1.0' encoding='utf-8'?><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><createsessionresponse soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"><createsessionreturn xsi:type="xsd:string">59C3F170141E9CF6F5BF98FB39B0237B</createsessionreturn></createsessionresponse></soapenv:body></soapenv:envelope>"""
>>> obj = lxml.objectify.fromstring(doc)
>>> obj
<Element {http://schemas.xmlsoap.org/soap/envelope/}envelope at 0x2978eb0>
>>> for e in obj.iter():
... print repr(e)
...
<Element {http://schemas.xmlsoap.org/soap/envelope/}envelope at 0x2978eb0>
<Element {http://schemas.xmlsoap.org/soap/envelope/}body at 0x2978f50>
<Element createsessionresponse at 0x297c050>
'59C3F170141E9CF6F5BF98FB39B0237B'
>>>
lxml.objectify documentation mentions:
Lookups, however, inherit namespaces implicitly:
so obj.body.createsessionresponse.createsessionreturn
won't work
>>> obj.body
<Element {http://schemas.xmlsoap.org/soap/envelope/}body at 0x2978f00>
>>> obj.body.createsessionresponse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lxml.objectify.pyx", line 218, in lxml.objectify.ObjectifiedElement.__getattr__ (src/lxml/lxml.objectify.c:3488)
File "lxml.objectify.pyx", line 437, in lxml.objectify._lookupChildOrRaise (src/lxml/lxml.objectify.c:5743)
AttributeError: no such child: {http://schemas.xmlsoap.org/soap/envelope/}createsessionresponse
>>>
Still in the documentation
To access an element in a different namespace than its parent, you can use getattr():
For convenience, there is also a quick way through item access:
c = root["{http://other/}c"]
Applied to your case it becomes:
>>> obj.body["{}createsessionresponse"]
<Element createsessionresponse at 0x2978f50>
>>> obj.body["{}createsessionresponse"].createsessionreturn
'59C3F170141E9CF6F5BF98FB39B0237B'
>>>
>>> obj.body["{}createsessionresponse"].createsessionreturn
'59C3F170141E9CF6F5BF98FB39B0237B'
>>> type(obj.body["{}createsessionresponse"].createsessionreturn)
<type 'lxml.objectify.StringElement'>
>>> obj.body["{}createsessionresponse"].createsessionreturn.text
'59C3F170141E9CF6F5BF98FB39B0237B'
>>> type(obj.body["{}createsessionresponse"].createsessionreturn.text)
<type 'str'>
>>>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments