Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向ElementTree生成的XML添加SOAP信封_Python_Xml_Soap - Fatal编程技术网

Python 向ElementTree生成的XML添加SOAP信封

Python 向ElementTree生成的XML添加SOAP信封,python,xml,soap,Python,Xml,Soap,我从数据库中读取了一些数据,使用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"

我从数据库中读取了一些数据,使用
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>

UCR201700043926
9
TIS
CPS
1.
xxxxxxx
xxxxxx
现在,在使用请求将XML发布到web服务之前,我需要将其封装在SOAP信封中。如何使我的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>

xxxxxx
9
SGI
CPS
1.
xxx
xxxxxx
]]>

Python的标准
ElementTree
库不支持CDATA节,因此您需要确保使用的是
lxml
。假设您已经将
元素保存为字符串,这将为您提供所需内容:

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。传递到
元素
子元素
(另一个
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>

我后来设法解决了这个问题

    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>'''
data=''
'''
我使用了
.format(d)
函数

my
d
是由
d=ET.tostring(root,encoding='utf8',method='XML')
发帖的时候我刚打过电话
requests.post(url,data=data.format(d),headers=headers,verify=True)