用Python打印出xml数据?

时间:2016-12-01 13:35:29

标签: python xml soap

我正在尝试调用一个Web服务,它使用Python返回数据库连接字符串。我需要从xml打印出特定数据并将其存储在字符串中。我该怎么做?

import requests

url="http://172.10.3.2:8250/GS/GetConnectionStrings.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?xml version='1.0' encoding='utf-8'?>
                            <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                              <soap:Body>
                                <DatabaseConnectionString xmlns='http://tempuri.org/'>
                                  <DatabaseName>ELMA</DatabaseName>
                                </DatabaseConnectionString>
                              </soap:Body>
                            </soap:Envelope>"""

response = requests.post(url,data=body,headers=headers)
print response.content

结果打印如下: -

<?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><DatabaseConnectionStringResponse xmlns="http://tempuri.org/">
            <DatabaseConnectionStringResult>Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=ElmaMobileCommerce
</DatabaseConnectionStringResult>
</DatabaseConnectionStringResponse>
</soap:Body>
</soap:Envelope>

我需要打印出以下内容并将其存储在字符串中; -

<DatabaseConnectionStringResult>Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=ElmaMobileCommerce</DatabaseConnectionStringResult>

2 个答案:

答案 0 :(得分:1)

试试这个

import xml.etree.ElementTree as ET
root = ET.fromstring(body)
print root[0][0][0].text

输出看起来像这样

Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=ElmaMobileCommerce

答案 1 :(得分:0)

以下是我使用lxml

所做的事情
from lxml import etree

parser = etree.XMLParser(remove_blank_text=True, remove_comments=True)
root = etree.fromstring(body, parser=parser)
elem = root.find(".//{http://tempuri.org/}DatabaseConnectionStringResult")
xmlstr = etree.tostring(elem)

输出:

<DatabaseConnectionStringResult xmlns="http://tempuri.org/" 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">Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=ElmaMobileCommerce\n</DatabaseConnectionStringResult>

变异:

etree.tostring(elem, method='text')

将输出:

Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=ElmaMobileCommerce

编辑 - 更新以仅提取DatabaseConnectionStringResult