我正在尝试连接Python和Salesforce。
Salesforce向我发送了一条出站SOAP消息,我可以正确接收,确认和阅读该消息。
现在,我想解析消息以确定在Python中触发什么脚本。
以下是收到的邮件正文的印刷示例:(我用XXX标识了ID)
b'<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
<OrganizationId>00Dxxxx</OrganizationId>
<ActionId>04k5A000000XXX</ActionId>
<SessionId xsi:nil="true"/>
<EnterpriseUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/c/45.0/00Dxxxx </EnterpriseUrl>
<PartnerUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/u/45.0/00Dxxxx </PartnerUrl>
<Notification>
<Id>04l5A000XXX</Id>
<sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>a0B5A0XXX</sf:Id>
<sf:Status__c>Send price request</sf:Status__c>
</sObject>
</Notification>
</notifications>
</soapenv:Body>
</soapenv:Envelope>'
str前面的b是什么?当我打印消息正文时,Python会打印它。有影响吗?
现在,要处理我的消息,我想阅读sObject选项卡中的行,即示例中的行:
<sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>a0B5A0XXX</sf:Id>
<sf:Status__c>Send price request</sf:Status__c>
</sObject>
有时候,我希望我的消息具有除“状态”和“发送的ID”以外的其他字段,我希望将消息正确地解析到表中,然后根据所发送的字段及其值确定要触发的操作。
我将管理一个包含字段名称/字段值和要触发的操作的表,这对我来说似乎很容易。
正确正确地动态读取此消息的最佳方法是什么?
答案 0 :(得分:1)
在下面看看
import re
import xml.etree.ElementTree as ET
XML = '''<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
<OrganizationId>00Dxxxx</OrganizationId>
<ActionId>04k5A000000XXX</ActionId>
<SessionId xsi:nil="true"/>
<EnterpriseUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/c/45.0/00Dxxxx </EnterpriseUrl>
<PartnerUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/u/45.0/00Dxxxx </PartnerUrl>
<Notification>
<Id>04l5A000XXX</Id>
<sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>a0B5A0XXX</sf:Id>
<sf:Status__c>Send price request</sf:Status__c>
</sObject>
</Notification>
</notifications>
</soapenv:Body>
</soapenv:Envelope>'''
_xml = xmlstring = re.sub(' xmlns="[^"]+"', '', XML, count=1)
tree = ET.fromstring(_xml)
sobject = tree.find('.//sObject')
for idx, child in enumerate(list(sobject)):
print('{}) {} => {}'.format(idx, child.tag[child.tag.find('}') + 1:], child.text))
输出
0) Id => a0B5A0XXX
1) Status__c => Send price request
答案 1 :(得分:0)
使用xmltodict找到了我的方式
import pandas as pd
import xmltodict
#Convert the msg to dict
ParsedSFMessage = xmltodict.parse(sfmsg)
#Keep only the needed level
ParsedSFMessage = ParsedSFMessage['soapenv:Envelope']['soapenv:Body']['notifications']['Notification']['sObject']
#Convert to df and transpose
ParsedSFMessage = pd.DataFrame.from_dict(ParsedSFMessage, orient='index').T.drop(columns = ['@xmlns:sf'])
Etvoilà
@xsi:type sf:Id sf:Status__c
sf:QuoteHistory__c a0B5A0XXXX Send price request