如何使用Python ElementTree,LXML或类似库创建肥皂请求 [英] How to create a soap request using Python ElementTree, LXML or similar library
问题描述
我正在尝试使用Excel工作表中的数据创建XML SOAP请求。当前,我已经使用了mako模板,但是它需要XML模板。如何使用如下所示的命名空间创建请求(这只是一个小样本,而不是完整的XML):
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>
通过使用lxml库,我可以取得一些进步,但是后来我陷入了困境。下面是我创建的代码。
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))
推荐答案
您正在错误地构造Envelope元素。
etree.Element 的构造函数接收要创建的xml元素的(完全限定)名称(aka标记),在您的情况下为 Envelope 。
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.
将第一行更改为此:
envelope = etree.Element("{http://www.w3.org/1999/soapenv}Envelope")
这篇关于如何使用Python ElementTree,LXML或类似库创建肥皂请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!