使用Python解析XML SOAP响应

时间:2013-02-12 08:28:31

标签: python xml soap

我想从SOAP解析此响应并在<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 Libs做到这一点?

2 个答案:

答案 0 :(得分:6)

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

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


>>45eeadF43423KKmP33

而不是打印,做一些有用的事情。

答案 1 :(得分:0)

我的格式相同,但有多个子标签,如下所示。 如何提取数据。

<LoginResponse xmlns="http://tempuri.org/wsSalesQuotation/Service1">
        <LoginResult>45eeadF43423KKmP33</LoginResult>
</LoginResponse>
<LoginResponse xmlns="http://tempuri.org/wsSalesQuotation/Service1">
        <LoginResult>45eeadF43423KKmP33</LoginResult>
</LoginResponse>
相关问题