如何在SUDS(python)中向结构添加元素?

时间:2014-06-11 09:43:18

标签: python xml soap suds

我创建了AccountAssignment结构

client = suds.client.Client(url)
accountAssignment = client.factory.create('AccountAssignment')
print 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
 }

然后我可以设置所需的值并将该结构发送到适当的方法。问题是,结构中缺少两个元素。 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?
 }

我尝试使用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

0 个答案:

没有答案
相关问题