2017-05-30 79 views
1

我已經從數據庫中讀取一些數據,並使用xml.etree.ElementTree我能夠使用此代碼生成XML:添加SOAP信封將ElementTree生成的XML

top = ET.Element("Enquiry") 
child = ET.SubElement(top, 'DocumentHeader') 

msgid = ET.SubElement(child, 'msgid') 
msgid.text = "4444444" 

refno = ET.SubElement(child, 'refno') 
refno.text = "xxxxxx" 

msg_func = ET.SubElement(child, 'msg_func') 
msg_func.text = "9" 

#... 

tree = ET.ElementTree(top) 
root = tree.getroot() 
data = ET.tostring(root, encoding='utf8', method='xml') 
print data 

這會產生這樣的XML:

<Enquiry> 
    <DocumentHeader> 
     <msgid></msgid> 
       <refno>UCR201700043926</refno> 
       <msg_func>9</msg_func> 
       <sender>TIS</sender> 
       <receiver>CPS</receiver> 
       <version>1</version> 
       </DocumentHeader> 
     <DocumentDetails> 
        <ucr_no>xxxxxxx</ucr_no> 
        <token>xxxxxx</token> 
     </DocumentDetails> 
</Enquiry> 

現在我需要將XML封裝到SOAP信封中,然後使用請求將其發佈到Web服務中。如何讓我的XML看起來是一樣的,因爲這:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.ucr.oga.kesws.crimsonlogic.com/"> 
     <soapenv:Header/> 
     <soapenv:Body> 
      <web:ucrValidation> 
      <arg0><![CDATA[ 
       <UCR_Enquiry> 
        <DocumentHeader> 
         <msgid></msgid> 
         <refno>xxxxxx</refno> 
         <msg_func>9</msg_func> 
         <sender>SGI</sender> 
         <receiver>CPS</receiver> 
         <version>1</version> 
        </DocumentHeader> 
        <DocumentDetails> 
         <ucr_no>xxx</ucr_no> 
         <token>xxxxxx</token> 
        </DocumentDetails> 
       </UCR_Enquiry> 
      ]]></arg0> 
      </web:ucrValidation> 
     </soapenv:Body> 
    </soapenv:Envelope> 

回答

0

我管理稍後解決此問題。

data = ''' 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.ucr.oga.kesws.crimsonlogic.com/"> 
     <soapenv:Header/> 
     <soapenv:Body> 
      <web:ucrValidation> 
      <arg0><![CDATA[ 
      {0} 
      ]]></arg0> 
      </web:ucrValidation> 
     </soapenv:Body> 
    </soapenv:Envelope>''' 

我用.format(d)功能

d通過d = ET.tostring(root, encoding='utf8', method='xml') 所產生的XML同時發佈我剛打電話 requests.post(url, data=data.format(d), headers=headers, verify=True)

1

Python的標準ElementTree庫不支持CDATA部分,所以你需要確保你使用lxml。假設你已經有<Enquiry>元素保存爲一個字符串,這會給你,你找什麼:

from lxml import etree as ET 

SOAP_NS = 'http://schemas.xmlsoap.org/soap/envelope/' 
WEB_NS = 'http://webservice.ucr.oga.kesws.crimsonlogic.com/' 
ns_map = {'soapenv': SOAP_NS, 'web': WEB_NS} 

env = ET.Element(ET.QName(SOAP_NS, 'Envelope'), nsmap=ns_map) 
head = ET.SubElement(env, ET.QName(SOAP_NS, 'Header'), nsmap=ns_map) 
body = ET.SubElement(env, ET.QName(SOAP_NS, 'Body'), nsmap=ns_map) 
val = ET.SubElement(body, ET.QName(WEB_NS, 'ucrValidation'), nsmap=ns_map) 
arg = ET.SubElement(val, 'arg0') 
arg.text = ET.CDATA('Here is where you can put your CDATA text!!!') 

# now you have XML! 
print(ET.tostring(env, pretty_print=True)) 

我使用QName函數來創建元素名稱,包括命名空間URI。傳遞給ElementSubElement(另一lxml擴展)的命名空間映射URI映射到一個前綴,其用於輸出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.ucr.oga.kesws.crimsonlogic.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <web:ucrValidation> 
     <arg0><![CDATA[Here is where you can put your CDATA text!!!]]></arg0> 
    </web:ucrValidation> 
    </soapenv:Body> 
</soapenv:Envelope>