![](/img/trans.png)
[英]Python - Suds. How to edit the namespaces of SOAP-ENV:Envelope
[英]Python - How to edit the namespaces of Soap Envelope / xmlns:encodingStyle
為 SOAP 調用創建 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)
因此,我得到以下 XML 文件:
<soap:Envelope xmlns:soap="URL1" xmlns:encodingStyle="URL2/soap-encoding">
<soap:Body>
<Data type="import">1234</Data>
</soap:Body>
</soap:Envelope>
我需要創建的 XML 文件的第一行必須是這樣的
<soap:Envelope xmlns:soap="URL1" soap:encodingStyle="URL2/soap-encoding">
<soap:Body>
<Data type="import">1234</Data>
</soap:Body>
</soap:Envelope>
如何將 URL 2 的前綴/命名空間從xmlns:encodingStyle更改為soap:encodingStyle或者如果我的方法錯誤,如何將 ZCBF4E0B7971051750907Z:encodingStyleE 添加到 encodingStyle?
提前致謝
soap:encodingStyle
是綁定到命名空間的屬性。 使用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.