问题
I am trying to create XML SOAP request using data from excel sheet. Currentlly, I have used mako templates but it requires XML template. How do I create a request with namespace like below (this is just a small sample not the complete XML):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mes="http://www.orange.com/Webalc/Interfaces/ManageSupplierQuote/RequestForQuotation/v3/message">
<soapenv:Header/>
<soapenv:Body>
<mes:CalculateSupplierQuote>
<!--1 to 500 repetitions:-->
<SupplierQuote>
<local_circuittype>Existing circuit</local_circuittype>
<local_businessOpportunity>Access to Orange Business Services Network</local_businessOpportunity>
<local_accessType>Upgrade/downgrade of full path diversity</local_accessType>
<!--Optional:-->
<local_configurationSite>single</local_configurationSite>
By using lxml library I am able to make some progress but then I am stuck. Below is the code that I have created.
from lxml import etree
import lxml.etree
import lxml.builder
Envelope = etree.Element("{http://www.w3.org/1999/soapenv}xmlns")
body = etree.SubElement(Envelope, "{http://www.w3.org/1999/soapenv}body")
print(etree.tostring(Envelope, pretty_print=True))
回答1:
You are constructing the Envelope element wrong. The constructor of etree.Element receives the (fully qualified) name (a.k.a tag) of the xml element you want to create, which is in your case Envelope.
Change the first line to this:
envelope = etree.Element("{http://www.w3.org/1999/soapenv}Envelope")
来源:https://stackoverflow.com/questions/43494942/how-to-create-a-soap-request-using-python-elementtree-lxml-or-similar-library