Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing Soap XML response
#1
Hi,

I'm a Python newbie.
Still learning and following tutorials etc.

At this moment i'm trying to learn REST/SOAP api's.
I'm able to parse json results, but i'm not able to find a piece of code which can parse my XML response.

I want to retrieve "result" and "sessionId"

Would be nice if someone can help me or point me to the right direction,

Thanks in advance,

Richard


XML Response:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns2:loginResponse xmlns:ns2="http://xyz/gss/session/SessionService">
<ns2:return result="true" sessionId="7111510180479788278"/>
</ns2:loginResponse>
</soapenv:Body>
</soapenv:Envelope>
Reply
#2
Can do it with BeautifulSoup.
Example.
from bs4 import BeautifulSoup

xml = '''\
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns2:loginResponse xmlns:ns2="http://xyz/gss/session/SessionService">
<ns2:return result="true" sessionId="7111510180479788278"/>
</ns2:loginResponse>
</soapenv:Body>
</soapenv:Envelope>'''

soup = BeautifulSoup(xml, 'xml')
Test.
>>> ns2 = soup.find('ns2:return')
>>> ns2
<ns2:return result="true" sessionId="7111510180479788278"/>

>>> ns2.attrs
{'result': 'true', 'sessionId': '7111510180479788278'}
>>> ns2.attrs['sessionId']
'7111510180479788278'
Reply
#3
Thanks for helping me.

i'm trying to use ure examples but I reveive an error message.

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser


Any idea how to solve this?

Richard
Reply
#4
(Jun-13-2019, 09:26 PM)grootkarzijn Wrote: Do you need to install a parser
Yes as i use lxml’s xml parser.
pip install lxml
Reply
#5
Thanks!! It works!!

But now I'm strugling with another response.
Lot's of examples and tutorials, but none of the responses I have found is suitable.

I hope you can help me out.

Output:
<?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <ns4:getIcpIdResponse xmlns:ns4="http://xyz.com/abc/cc/standard"> <ns4:return result="true" Id="2886886520"> <ns1:connectionState xmlns:ns1="http://xyz.com/abc/cc">SUCCESS</ns1:connectionState> <ns2:icpVersion xmlns:ns2="http://xyz.com/abc/cc">5.0 SP2 </ns2:icpVersion> </ns4:return> </ns4:getIcpIdResponse> </soapenv:Body> </soapenv:Envelope>
result and Id is no problem now, but I want to have the value "SUCCESS"
I hope you will help me again.

So far i tried: soup.find('ns1:connectionState').attrs['xmlns:ns1']
This is resulting in : http://xyz.com/abc/cc

Thanks in advance

Richard
Reply
#6
>>> soup.find('ns1:connectionState').text
'SUCCESS'
Reply
#7
Thanks!!

Really appreciate your effort of helping me.

I hope you will help me for the last time.
I marked the items i want to have with "*------------------"
Probably are the methods the same.

Please help me with this.

After this I have enough homework to do some reverse engineering.


Output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <ns4:getEventResponse xmlns:ns4="http://abc.com/rg/cc/standard/StandardCCService"> <ns4:return result="true"> <ns2:eventType xmlns:ns2="http://mitel.com/oig/cc">CALL_EVENT</ns2:eventType> <callEvent xmlns="http://abc.com/rg/cc" localCallId="2317" callEventTime="1560466816084" objectId="115916840915166"> *------------------<type>CALL_RECEIVED</type> *------------------<cause>NEW_CALL</cause> *------------------<callState>RECEIVED</callState> <callEventAttribute> <attributeName>CURRENT_GLOBAL_CALL_ID</attributeName> <attributeValue>9330E9CF9C423A070458</attributeValue> <attributeType>STRING</attributeType> </callEventAttribute> <callEventAttribute> <attributeName>DIALED_DIGITS</attributeName> *------------------ <attributeValue>104</attributeValue> <attributeType>STRING</attributeType> </callEventAttribute> <callEventAttribute> <attributeName>ICP_ID</attributeName> <attributeValue>2886886520</attributeValue> <attributeType>STRING</attributeType> </callEventAttribute> <featuresAllowed>ANSWER_CALL</featuresAllowed> <featuresAllowed>REDIRECT_CALL</featuresAllowed> <device> <deviceName>CALLED_DEVICE</deviceName> <deviceAttribute> <attributeName>NUMBER</attributeName> *------------------ <attributeValue>104</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> <deviceAttribute> <attributeName>NAME</attributeName> <attributeValue>name1</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> <deviceAttribute> <attributeName>DEVICE_TYPE</attributeName> <attributeValue>INTERNAL_DEVICE_TYPE</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> </device> <device> <deviceName>CALLING_DEVICE</deviceName> <deviceAttribute> <attributeName>NUMBER</attributeName> *------------------ <attributeValue>103</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> <deviceAttribute> *------------------ <attributeName>NAME</attributeName> <attributeValue>name2</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> <deviceAttribute> <attributeName>DIALED_DIGITS</attributeName> <attributeValue>104</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> <deviceAttribute> <attributeName>CALL_ID</attributeName> <attributeValue>2219</attributeValue> <attributeType>LONG_INTEGER</attributeType> </deviceAttribute> <deviceAttribute> <attributeName>DEVICE_TYPE</attributeName> <attributeValue>INTERNAL_DEVICE_TYPE</attributeValue> <attributeType>STRING</attributeType> </deviceAttribute> </device> </callEvent> </ns4:return> </ns4:getEventResponse> </soapenv:Body> </soapenv:Envelope>
Richard
Reply
#8
(Jun-14-2019, 12:32 AM)grootkarzijn Wrote: Probably are the methods the same.
Then you could have tried Dodgy
>> soup.find('type').text
'CALL_RECEIVED'

>>> s = soup.find_all('device')
>>> s[0].find_all('attributeValue')
[<attributeValue>104</attributeValue>,
 <attributeValue>name1</attributeValue>,
 <attributeValue>INTERNAL_DEVICE_TYPE</attributeValue>]

>>> [tag.text for tag in s[0].find_all('attributeValue')]
['104', 'name1', 'INTERNAL_DEVICE_TYPE']
>>> [tag.text for tag in s[1].find_all('attributeValue')]
['103', 'name2', '104', '2219', 'INTERNAL_DEVICE_TYPE']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Zeep lib, encrypt soap body miha1234 0 2,811 Sep-12-2019, 07:52 AM
Last Post: miha1234
  Issues parsing the response from a request garnold 3 2,492 May-14-2019, 12:39 PM
Last Post: snippsat
  SOAP based webservice invoke mehrdadabedi 1 2,305 Mar-10-2019, 06:04 PM
Last Post: mlieqo
  How to retrieve and save attachments returned by SOAP web service ashwsrin 0 2,577 Feb-20-2019, 07:12 AM
Last Post: ashwsrin
  SOAP/XML input Help duguarun 0 2,446 Jun-08-2018, 10:20 AM
Last Post: duguarun
  extracting data from XML/SOAP hey_arnold 4 4,679 May-07-2018, 09:23 AM
Last Post: hey_arnold
  Soap json program rahul208in 5 3,817 Jan-11-2018, 05:28 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020