[英]python 3 - zeep - soap - 'Element Value from namespace xxx cannot have child contents to be deserialized as an object'
我的 wsdl 上有這個方法
<xs:element name="createDocument">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="repositoryId" nillable="true" type="xs:string"/>
<xs:element xmlns:q7="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="properties" nillable="true" type="q7:ArrayOfKeyValueOfstringanyType"/>
<xs:element xmlns:q8="http://docs.oasis-open.org/ns/cmis/core/200908/" minOccurs="0" name="contentStream" nillable="true" type="q8:ContentStream"/>
<xs:element xmlns:q9="http://docs.oasis-open.org/ns/cmis/core/200908/" minOccurs="0" name="versioningState" type="q9:VersioningState"/>
<xs:element xmlns:q10="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="policies" nillable="true" type="q10:ArrayOfstring"/>
</xs:sequence>
</xs:complexType>
</xs:element>
我需要用 python3 zeep 庫調用。
這就是我寫的
def test_createDocument():
c_stream = file_to_b64bytes("assets/pdv/packagePdV.zip")
c_length = len(c_stream)
answer = client.service.createDocument(
repositoryId="1",
properties=[
{
"KeyValueOfstringanyType": {
"Key": "PdV_VerificaFirmaFiles",
"Value": False
}
},
{
"KeyValueOfstringanyType": {
"Key": "Firma_OggettiProduttore",
"Value": False
}
}
],
contentStream={
"filename": "packagePdV.zip",
"length": c_length,
"stream": c_stream
}
)
print(answer)
但我有一個例外
zeep.exceptions.Fault:格式化程序在嘗試反序列化消息時拋出異常:嘗試反序列化參數http://tempuri.org/:properties時出錯。 InnerException 消息是“來自命名空間http://schemas.microsoft.com/2003/10/Serialization/Arrays的元素值不能將子內容反序列化為 object。 請使用 XmlNode[] 反序列化 XML.' 的這種模式。 有關更多詳細信息,請參閱 InnerException。
有什么建議嗎?
我遇到了同樣的問題,但不想離開 Zeep。
經過大量調試和比較 Zeep 生產的 XML 和 web 服務所需的 XML 后,我想到 Zeep 沒有在生產的 XML 中指定數據類型“布爾”。
這可以通過指定'Value': xsd.AnyObject(xsd.Boolean(), False)
作為PdV_VerificaFirmaFiles
的值。
所以,最后,這段代碼對我有用:
from zeep import xsd
....
soap_response = soap_client.service.createDocument(
repositoryId=1,
properties=[{
'KeyValueOfstringanyType': {'Key': 'PdV_VerificaFirmaFiles',
'Value': xsd.AnyObject(xsd.Boolean(), False)}
}],
contentStream={
'filename': 'PdVDocumento.zip',
'length': zip_size,
'stream': zip_file_contents
}
)
為了解決這個問題,我將庫從 zeep 更改為https://suds-py3.readthedocs.io/en/latest/因為它允許在通過插件發送之前更改 xml 消息。
我也嘗試使用醫生添加命名空間但沒有成功,
這是代碼
class FixTypes(MessagePlugin):
def marshalled(self, context):
context.envelope.getChild('Body').getChild('createDocument').getChild('properties')[0].getChild('Value').set('xmlns:c','http://www.w3.org/2001/XMLSchema')
context.envelope.getChild('Body').getChild('createDocument').getChild('properties')[0].getChild('Value').set('i:type','c:boolean')
context.envelope.getChild('Body').getChild('createDocument').getChild('properties')[1].getChild('Value').set('xmlns:c','http://www.w3.org/2001/XMLSchema')
context.envelope.getChild('Body').getChild('createDocument').getChild('properties')[1].getChild('Value').set('i:type','c:boolean')
context.envelope.getChild('Body').getChild('createDocument').getChild('properties').set('xmlns:i','http://www.w3.org/2001/XMLSchema-instance')
client = Client("", username="", password="", plugins=[FixTypes()])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.