Reputation:
I am trying to call a web service that returns me the Database Connection String using Python. I need to print out a specific data from the xml and store it in a string. How do i do it?
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
The result is printed as follows:-
<?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>
I need to print out the following specifically and store it in a string ;-
<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>
Upvotes: 1
Views: 1115
Reputation: 4532
Here is what I've done using 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)
output:
<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>
Variation:
etree.tostring(elem, method='text')
will output:
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
edit -- updated to pull out only the DatabaseConnectionStringResult
Upvotes: 0
Reputation: 3146
Try this
import xml.etree.ElementTree as ET
root = ET.fromstring(body)
print root[0][0][0].text
The output looks like this
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
Upvotes: 1