Mat*_*Mat 9 python xml soap suds
我创建了AccountAssignment结构
client = suds.client.Client(url)
accountAssignment = client.factory.create('AccountAssignment')
print accountAssignment
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
(AccountAssignment){
Key = None
AccountNo = None
TaxCode = None
Debit = None
Credit = None
Turnover = None
Text = None
FCDebit = None
FCCredit = None
FCTurnover = None
SummaryAccount = None
Balance = None
BankNo = None
BanksortingCode = None
BankAccountNo = None
DepositFormNo = None
DepositFormDate = None
ChequeNumber = None
ExpiryDate = None
GroupMovementType = None
AssociatedCompany = None
Comment1 = None
Comment2 = None
Comment3 = None
AdditionalDate1 = None
AdditionalDate2 = None
}
Run Code Online (Sandbox Code Playgroud)
然后我能够设置所需的值并将该结构发送到适当的方法.问题是,结构中缺少两个元素.AccountAssignment应如下所示:
(AccountAssignment){
Key = None
AccountNo = None
TaxCode = None
Debit = None
Credit = None
Turnover = None
Text = None
FCDebit = None
FCCredit = None
FCTurnover = None
SummaryAccount = None
Balance = None
BankNo = None
BanksortingCode = None
BankAccountNo = None
DepositFormNo = None
DepositFormDate = None
ChequeNumber = None
ExpiryDate = None
GroupMovementType = None
AssociatedCompany = None
Comment1 = None
Comment2 = None
Comment3 = None
AdditionalDate1 = None
AdditionalDate2 = None
DebitSpecified = None ** how to add this?
CreditSpecified = None ** how to add this?
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用MessagePlugin添加缺少的元素,并使用marshalled()方法.结果是,元素在发送之前添加,这为时已晚.我需要为两个缺少的元素设置值,因此在调用sending-method之前必须存在元素.我还尝试了InitPlugin和DocumentPlugin,没有运气.有任何想法吗?
解:
我通过使用DocumentPlugin并实现其parsed()方法解决了这个问题.既然似乎没有人有任何反对意见,我只提供一些代码,但不要再进一步评论.我失去了足够的时间.
class AccountAssignmentPlugin(suds.plugin.DocumentPlugin):
"""Plugin to add element to AccountAssignment that is not defined in WSDL."""
def __init__(self, *args):
self.args = args
def parsed(self, context):
# See documentation at http://jortel.fedorapeople.org/suds/doc/suds.sax.element.Element-class.html
# and http://jortel.fedorapeople.org/suds/doc/suds.sax.attribute.Attribute-class.html
complexTypes = context.document.getRoot().getChild('types').getChild('schema').getChildren('complexType')
# Get the complex type with attribute 'name' == 'AccountAssignment'.
for ct in complexTypes:
if ct.get('name') == 'AccountAssignment':
accountAssignment = ct
# Get the elements of the AccountAssignment that are used as attributes of the AttributeAssignment object.
sequenceElements = accountAssignment.getChild('complexContent').getChild('extension').getChild('sequence')
for key in self.args:
# Create new element (node)
e = suds.sax.element.Element('element')
e.setPrefix('s')
# Add attributes
a = suds.sax.attribute.Attribute('maxOccurs')
a.setValue(1)
e.append(a)
a = suds.sax.attribute.Attribute('type')
a.setValue('s:boolean')
e.append(a)
a = suds.sax.attribute.Attribute('name')
a.setValue(key)
e.append(a)
a = suds.sax.attribute.Attribute('minOccurs')
a.setValue(0)
e.append(a)
sequenceElements.append(e)
def transactionService(self):
module_name = 'WS3Trans'
service_name = 'TransactionService'
service_url = '%s/%s/%s.asmx?WSDL' %(self.webservice_url, module_name, service_name)
client = suds.client.Client(service_url, plugins=[AccountAssignmentPlugin('DebitSpecified', 'CreditSpecified')])
return client
Run Code Online (Sandbox Code Playgroud)
解决方案:
我通过使用 DocumentPlugin 并实现其方法 parsed() 解决了这个问题。由于似乎没有人感兴趣,我只提供一些代码,但不再进一步评论。我在这件事上浪费了足够多的时间。
class AccountAssignmentPlugin(suds.plugin.DocumentPlugin):
"""Plugin to add element to AccountAssignment that is not defined in WSDL."""
def __init__(self, *args):
self.args = args
def parsed(self, context):
# See documentation at http://jortel.fedorapeople.org/suds/doc/suds.sax.element.Element-class.html
# and http://jortel.fedorapeople.org/suds/doc/suds.sax.attribute.Attribute-class.html
complexTypes = context.document.getRoot().getChild('types').getChild('schema').getChildren('complexType')
# Get the complex type with attribute 'name' == 'AccountAssignment'.
for ct in complexTypes:
if ct.get('name') == 'AccountAssignment':
accountAssignment = ct
# Get the elements of the AccountAssignment that are used as attributes of the AttributeAssignment object.
sequenceElements = accountAssignment.getChild('complexContent').getChild('extension').getChild('sequence')
for key in self.args:
# Create new element (node)
e = suds.sax.element.Element('element')
e.setPrefix('s')
# Add attributes
a = suds.sax.attribute.Attribute('maxOccurs')
a.setValue(1)
e.append(a)
a = suds.sax.attribute.Attribute('type')
a.setValue('s:boolean')
e.append(a)
a = suds.sax.attribute.Attribute('name')
a.setValue(key)
e.append(a)
a = suds.sax.attribute.Attribute('minOccurs')
a.setValue(0)
e.append(a)
sequenceElements.append(e)
def transactionService(self):
module_name = 'WS3Trans'
service_name = 'TransactionService'
service_url = '%s/%s/%s.asmx?WSDL' %(self.webservice_url, module_name, service_name)
client = suds.client.Client(service_url, plugins=[AccountAssignmentPlugin('DebitSpecified', 'CreditSpecified')])
return client
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1686 次 |
| 最近记录: |