简体   繁体   English

Python - 如何编辑 Soap 信封 / xmlns:encodingStyle 的命名空间

[英]Python - How to edit the namespaces of Soap Envelope / xmlns:encodingStyle

I have an issue while creating an XML-file for a SOAP Call为 SOAP 调用创建 XML 文件时遇到问题

I am using the following code to create the XML:我正在使用以下代码创建 XML:

from lxml import etree as ET

SOAP_NS = "URL"
ENCODE_NS = "URL2/soap-encoding"
ns_map = {'soap' : SOAP_NS, 'encodingStyle' : ENCODE_NS}

root = ET.Element(ET.QName(SOAP_NS, 'Envelope'), nsmap=ns_map)
body = ET.SubElement(root, ET.QName(SOAP_NS, 'Body'), nsmap=ns_map)

Data = ET.SubElement(body, 'Data')
Data.text="1234"
Data.set('type','import')
xml_file = ET.ElementTree(root)
xml_file.write('Test.xml', pretty_print=True)

Thus I get the following XML-file:因此,我得到以下 XML 文件:

<soap:Envelope xmlns:soap="URL1" xmlns:encodingStyle="URL2/soap-encoding">
  <soap:Body>
    <Data type="import">1234</Data>
  </soap:Body>
</soap:Envelope>

The first line of the XML-file I need to create has to be like that我需要创建的 XML 文件的第一行必须是这样的

<soap:Envelope xmlns:soap="URL1" soap:encodingStyle="URL2/soap-encoding">
  <soap:Body>
    <Data type="import">1234</Data>
  </soap:Body>
</soap:Envelope>

How do I change the prefix/namespace of URL 2 from xmlns:encodingStyle to soap:encodingStyle or if my approach is wrong how do I add soap:encodingStyle to the envolope?如何将 URL 2 的前缀/命名空间从xmlns:encodingStyle更改为soap:encodingStyle或者如果我的方法错误,如何将 ZCBF4E0B7971051750907Z:encodingStyleE 添加到 encodingStyle?

Thanks in advance提前致谢

soap:encodingStyle is an attribute bound to a namespace. soap:encodingStyle是绑定到命名空间的属性。 Add it using the set() method.使用set()方法添加它。

from lxml import etree as ET
 
SOAP_NS = "URL"
ENCODE_NS = "URL2/soap-encoding"
ns_map = {'soap' : SOAP_NS}
 
root = ET.Element(ET.QName(SOAP_NS, 'Envelope'), nsmap=ns_map)
root.set(ET.QName(SOAP_NS, "encodingStyle"), ENCODE_NS)
 
body = ET.SubElement(root, ET.QName(SOAP_NS, 'Body'), nsmap=ns_map)
 
Data = ET.SubElement(body, 'Data')
Data.text="1234"
Data.set('type','import')
xml_file = ET.ElementTree(root)
xml_file.write('Test.xml', pretty_print=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM