使用python和suds,服务器端不会读取数据,因为元素并没有定义为数组



我是一个非常缺乏经验的程序员,没有受过正规教育。在任何回复中,细节都将非常有帮助。

我已经制作了几个基本的python脚本来调用SOAPAPI,但我遇到了一个特定的API函数的问题,该函数有一个嵌入的数组。

以下是一个工作XML格式的示例摘录,用于显示嵌套数据:

<bomData xsi:type="urn:inputBOM" SOAP-ENC:arrayType="urn:bomItem[]">
<bomItem>
<item_partnum></item_partnum>
<item_partrev></item_partrev>
<item_serial></item_serial>
<item_lotnum></item_lotnum>
<item_sublotnum></item_sublotnum>
<item_qty></item_qty>
</bomItem>
<bomItem>
<item_partnum></item_partnum>
<item_partrev></item_partrev>
<item_serial></item_serial>
<item_lotnum></item_lotnum>
<item_sublotnum></item_sublotnum>
<item_qty></item_qty>
</bomItem>
</bomData>

我尝试了三种不同的方法来让它发挥作用,但都无济于事。

我可以从脚本中生成近乎精确的XML,但缺少一个关键属性是"SOAP-ENC:arrayType="urn:bombItem[]"'在上面的XML示例中。

选项1使用了MessagePlugin,但我遇到了一个错误,因为我的部分类似于3元素,它总是注入到第一个元素中。我试过body[2],但这出了一个错误。

选项2我正在尝试创建对象(?(。我读了很多堆栈溢出,但我可能为此遗漏了一些内容。

选项3看起来很简单,但也失败了。我尝试直接在JSON中设置值。我通过JSON的XML示例获得了这些示例。

我还做了其他几件小事,试图让它发挥作用,但不值一提。尽管如此,如果有办法做到以下几点,那么我洗耳恭听:bomItem[]:bomData={quot;bomItem"[{…,…,…}]}

以下是我的脚本示例:

# for python 3
# using pip install suds-py3
from suds.client import Client
from suds.plugin import MessagePlugin
# Config
#option 1: trying to set it as an array using plugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
body = context.envelope.getChild('Body')
bomItem = body[0]
bomItem.set('SOAP-ENC:arrayType', 'urn:bomItem[]')
URL = "http://localhost/application/soap?wsdl"
client = Client(URL, plugins=[MyPlugin()])
transact_info = { 
"username":"",
"transaction":"",
"workorder":"",
"serial":"",
"trans_qty":"",
"seqnum":"",
"opcode":"",
"warehouseloc":"",
"warehousebin":"",
"machine_id":"",
"comment":"",
"defect_code":""
}
#WIP - trying to get bomData below working first
inputData = {
"dataItem":[
{
"fieldname": "",
"fielddata": ""
}
]
}
#option 2: trying to create the element here and define as an array
#inputbom = client.factory.create('ns3:inputBOM')
#inputbom._type = "SOAP-ENC:arrayType"
#inputbom.value = "urn:bomItem[]"
bomData = {
#Option 3: trying to set the time and array type in JSON
#"@xsi:type":"urn:inputBOM",
#"@SOAP-ENC:arrayType":"urn:bomItem[]",
"bomItem":[
{
"item_partnum":"",
"item_partrev":"",
"item_serial":"",
"item_lotnum":"",
"item_sublotnum":"",
"item_qty":""
},
{
"item_partnum":"",
"item_partrev":"",
"item_serial":"",
"item_lotnum":"",
"item_sublotnum":"",
"item_qty":""
}
]
}
try:
response = client.service.transactUnit(transact_info,inputData,bomData)
print("RESPONSE: ")
print(response)
#print(client)
#print(envelope)
except Exception as e:
#handle error here
print(e)

我感谢任何帮助,并希望它很容易解决。

我找到了我想要的答案。至少是一个有效的解决方案。

无论如何,备选方案1是可行的。我在以下链接上阅读了它:https://suds-py3.readthedocs.io/en/latest/您可以在'!MessagePlugin"部分。

我从下面的帖子中找到了一个让消息插件工作的解决方案:解组错误:对于输入字符串:"">

一位用户发布了一个如何遍历XML结构并对其进行修改的示例

这是我修改后的例子,让我的脚本工作:

#Using MessagePlugin to modify elements before sending to server
class MyPlugin(MessagePlugin):
# created method that could be reused to modify sections with similar 
# structure/requirements
def addArrayType(self, dataType, arrayType, transactUnit):
# this is the code that is key to crawling through the XML - I get 
# the child of each parent element until I am at the right level for 
# modification
data = transactUnit.getChild(dataType)
if data:
data.set('SOAP-ENC:arrayType', arrayType)
def marshalled(self, context):
# Alter the envelope so that the xsd namespace is allowed
context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'
body = context.envelope.getChild('Body')
transactUnit = body.getChild("transactUnit")
if transactUnit:
self.addArrayType('inputData', 'urn:dataItem[]', transactUnit)
self.addArrayType('bomData', 'urn:bomItem[]', transactUnit)

相关内容

最新更新