How can i access element value in SOAP response with lxml objectify

aabele

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 ?

paul trmbrth

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: How can I use lxml objectify's iterchildren to get details of siblings which are in a different namespace

From Dev

How to retrieve element value from SOAP response using Java?

From Dev

lxml parsing with python: how to with objectify

From Dev

lxml parsing with python: how to with objectify

From Dev

How can I send a SOAP request and receive a response using HTML?

From Dev

How can i send response message to SOAP Outbound Message in Salesforce

From Dev

lxml - Access value of meta tag on element

From Dev

lxml - Access value of meta tag on element

From Dev

Test the existence of an Element with lxml.objectify

From Dev

Objectify: How can I disable session cache?

From Dev

How do you create a non-nested xml element using Python's lxml.objectify?

From Dev

How to parse a value from an XML response which I got as a soap API response in PHP

From Dev

How can I access the children of a panel element?

From Dev

How can I access the host of a custom element

From Dev

how can I access to a specific element in json

From Dev

How can I access the host of a custom element

From Dev

How can I access the children of a panel element?

From Dev

How can I access a property of an html element?

From Dev

Change int element to str element in lxml objectify python

From Dev

Change int element to str element in lxml objectify python

From Dev

How can I access element reference from *ngFor loop element?

From Dev

How I can set the value of element of a fragment?

From Dev

How can I get the specific element value

From Dev

How I can set the value of element of a fragment?

From Dev

How to lookup element in AWS response with lxml (namespace issue?)

From Dev

How can i access a Value in an Array with jQuery

From Dev

How can I access a value in an object in javascript?

From Dev

how can i access the value of a stdclass in php

From Dev

How can I access value in sequence type?

Related Related

  1. 1

    Python: How can I use lxml objectify's iterchildren to get details of siblings which are in a different namespace

  2. 2

    How to retrieve element value from SOAP response using Java?

  3. 3

    lxml parsing with python: how to with objectify

  4. 4

    lxml parsing with python: how to with objectify

  5. 5

    How can I send a SOAP request and receive a response using HTML?

  6. 6

    How can i send response message to SOAP Outbound Message in Salesforce

  7. 7

    lxml - Access value of meta tag on element

  8. 8

    lxml - Access value of meta tag on element

  9. 9

    Test the existence of an Element with lxml.objectify

  10. 10

    Objectify: How can I disable session cache?

  11. 11

    How do you create a non-nested xml element using Python's lxml.objectify?

  12. 12

    How to parse a value from an XML response which I got as a soap API response in PHP

  13. 13

    How can I access the children of a panel element?

  14. 14

    How can I access the host of a custom element

  15. 15

    how can I access to a specific element in json

  16. 16

    How can I access the host of a custom element

  17. 17

    How can I access the children of a panel element?

  18. 18

    How can I access a property of an html element?

  19. 19

    Change int element to str element in lxml objectify python

  20. 20

    Change int element to str element in lxml objectify python

  21. 21

    How can I access element reference from *ngFor loop element?

  22. 22

    How I can set the value of element of a fragment?

  23. 23

    How can I get the specific element value

  24. 24

    How I can set the value of element of a fragment?

  25. 25

    How to lookup element in AWS response with lxml (namespace issue?)

  26. 26

    How can i access a Value in an Array with jQuery

  27. 27

    How can I access a value in an object in javascript?

  28. 28

    how can i access the value of a stdclass in php

  29. 29

    How can I access value in sequence type?

HotTag

Archive