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 parserYes 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. 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. 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 >> 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'] |