Skip to main content
 首页 » 编程设计

python之将 SOAP 信封添加到 ElementTree 生成的 XML

2025年02月15日8sky-heaven

我已经从数据库中读取了一些数据,并使用 xml.etree.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> 

请您参考如下方法:

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函数创建元素名称,包括 namespace URI。传递给 Element 的命名空间映射和 SubElement (另一个 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>