使用 Python 解析 XML SOAP 响应

问题描述:

我想从 SOAP 解析这个响应并提取 之间的文本:

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>

我如何使用 XML Python 库来实现?

How I can do it using XML Python Libs?

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.