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 文档提到:
然而,查找隐式继承命名空间:
所以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'>
>>>