2013-10-09 33 views
3

我有SOAP響應:如何訪問元素值與LXML 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的價值?

回答

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

查找,然而,隱式繼承命名空間:

所以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'> 
>>> 
+0

非常感謝您對這個答案。花了整整一個晚上。 – aabele