Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

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

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...