How to create a soap request using Python ElementTree, LXML or similar library

筅森魡賤 提交于 2019-12-12 06:57:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!