如何使用lxml objectify访问SOAP响应中的元素值

时间:2013-10-09 19:15:09

标签: python soap lxml objectify

我有SOAP响应:

<?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返回:

{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'

如何访问createSessionReturn的值?

1 个答案:

答案 0 :(得分:3)

createSessionResponse未使用http://schemas.xmlsoap.org/soap/envelope/命名空间

>>> 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提及:

  然而,

Lookups隐式地继承了命名空间:

所以obj.body.createsessionresponse.createsessionreturn无效

>>> 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
>>> 

仍在文档中

  

要访问与其父级不同的命名空间中的元素,您可以   使用getattr():

     

为方便起见,还有一条快捷的方法   商品访问权限:c = root["{http://other/}c"]

适用于您的情况,它变为:

>>> 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'>
>>>