4

I want parse this response from SOAP and extract text between <LoginResult> :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <soap:Body>
      <LoginResponse xmlns="http://tempuri.org/wsSalesQuotation/Service1">
        <LoginResult>45eeadF43423KKmP33</LoginResult>
      </LoginResponse>
     </soap:Body>
</soap:Envelope>

How I can do it using XML Python Libs?

0

1 Answer 1

10
import xml.etree.ElementTree as ET
tree = ET.parse('soap.xml')    

print tree.find('.//{http://tempuri.org/wsSalesQuotation/Service1}LoginResult').text


>>45eeadF43423KKmP33

instead of print, do something useful to it.

Sign up to request clarification or add additional context in comments.

7 Comments

SyntaxError: cannot use absolute path on element
What python version are you using?
I have Python 2.7.3 on MAC osX
but I use ET.fromstring(...) doesn't work, but parse() works. What's the problem with fromstring
Might be some encoding issue. Not sure. But try with that modification i made in the example. Add a "." infront of the //. Where you get the string from, straight from http-response?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.