Python Forum
Parsing Soap XML response - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Parsing Soap XML response (/thread-19086.html)



Parsing Soap XML response - grootkarzijn - Jun-12-2019

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>


RE: Parsing Soap XML response - snippsat - Jun-12-2019

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'



RE: Parsing Soap XML response - grootkarzijn - Jun-13-2019

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


RE: Parsing Soap XML response - snippsat - Jun-13-2019

(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



RE: Parsing Soap XML response - grootkarzijn - Jun-13-2019

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


RE: Parsing Soap XML response - snippsat - Jun-13-2019

>>> soup.find('ns1:connectionState').text
'SUCCESS'



RE: Parsing Soap XML response - grootkarzijn - Jun-14-2019

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


RE: Parsing Soap XML response - snippsat - Jun-14-2019

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