我正在使用spyne Array來轉換JSON列表,並且需要將「id」屬性添加到最終XML中的「referral」父節點。如何將屬性添加到Spyne Array中的所有XMLElements中
這是最後的XML我期待:
<viewOutboundResponse user="rayners">
<referral id="123">
<status>SUBMITTED</status>
<from>
<outlet id="12345">ABC</outlet>
</from>
<to>
<outlet id="6789">XYZ</outlet>
</to>
<date>2015-01-14</date>
<client>Bloggs</client>
<daysToExpiry>3</daysToExpiry>
</referral>
<referral id="456">
<status>REJECTED</status>
<from>
<outlet id="101112">DEF</outlet>
</from>
<to>
<outlet id="131415">S2X Demo</outlet>
</to>
<date>2004-01-15</date>
<client>Gobbs</client>
<daysToExpiry>7</daysToExpiry>
</referral>
</viewOutboundResponse>
這裏是我的代碼:
class ReferralSummaryType(ComplexModel):
__type_name__ = 'referral'
type_info = {'id': XmlAttribute(Integer),
'status': Unicode,
'from': ReferralFromType,
'to': ReferralToType,
'date': Date,
'client': Unicode,
'daysToExpiry': Integer}
class OutboundResponseType(ComplexModel):
__mixin__ = True
referral = Array(ReferralSummaryType)
但我得到的輸出:
<viewOutboundResponse user="rayners">
<referral>
<referral id="123">
<status>SUBMITTED</status>
<from>
<outlet id="12345">ABC</outlet>
</from>
<to>
<outlet id="6789">XYZ</outlet>
</to>
<date>2015-01-14</date>
<client>Bloggs</client>
<daysToExpiry>3</daysToExpiry>
</referral>
<referral id="456">
<status>REJECTED</status>
<from>
<outlet id="101112">DEF</outlet>
</from>
<to>
<outlet id="131415">S2X Demo</outlet>
</to>
<date>2004-01-15</date>
<client>Gobbs</client>
<daysToExpiry>7</daysToExpiry>
</referral>
</referral>
</viewOutboundResponse>