使用Python的XML库从Spring API访问特定行

fykwrbwg  于 2022-12-17  发布在  Spring
关注(0)|答案(1)|浏览(106)

我正在尝试使用python脚本从process-conf.xml访问bean的sfdc.extractionSOQ子行。
我的XML文件看起来像...

<beans>
...

    <bean id="BatchCustomerAccountExtract"
          class="com.salesforce.dataloader.process.ProcessRunner"
          scope="prototype">
      <description>BatchCustomerAccountExtract</description>
        <property name="name" value="BatchCustomerAccountExtract"/>
        <property name="configOverrideMap">
            <map>
                <entry key="sfdc.debugMessages" value="false"/>
                <entry key="sfdc.debugMessagesFile" value="Logs/"/>
                <entry key="sfdc.endpoint" value="https://login.salesforce.com"/>
                <entry key="sfdc.username" value="[omitted]"/>
                <entry key="process.encryptionKeyFile" value="key.txt"/>
                <entry key="sfdc.password" value="fakeValue"/>
                <entry key="sfdc.entity" value="Account"/>
                <entry key="sfdc.timeoutSecs" value="600"/>
                <entry key="sfdc.extractionRequestSize" value="1000"/>
                <entry key="process.operation" value="extract"/>
                <entry key="dataAccess.type" value="csvWrite"/>
                <entry key="process.enableExtractSuccessOutput" value="true"/>
                <entry key="process.outputError" value="BatchCustomerAccountExtract.csv"/>
                <entry key="dataAccess.name" value=".\Data\BatchContractUpdate.csv"/>
                <entry key="sfdc.extractionSOQL" value="
                SELECT Id, Name, Client_External_ID__c, Customer_Number__c, Account_RecordID__c, Portal_Access_Code__c
                FROM Account 
                WHERE Id IN ('[omitted]')"/>
            </map>
        </property>
    </bean>

...
</beans

我尝试使用XML库,但是我得到了一些值,我不确定它们是如何到达的。据我所知,我可以通过bean/description/property/map/访问所有相应的行,但是唉...
这是我尝试过的方法。

import xml.etree.ElementTree as ET

tree = ET.parse('process-conf.xml')
root = tree.getroot()    

main_bean = root.find(".//bean[@id='BatchCustomerAccountExtract']")
print(main_bean)                     #<Element 'bean' at 0x0000027D9324D440>

main_bean_description = main_bean.find(".//property[@name='description']")
print(main_bean_description)         #None

for att in root.findall("./bean/descriptionn/"):
    print(att)   #nothing prints ;(

如何使用python访问SOQL行?谢谢您的帮助。

svgewumm

svgewumm1#

下面的代码可以工作(我假设您正在寻找SQL短语)

import xml.etree.ElementTree as ET

xml = '''<beans>
    <bean id="BatchCustomerAccountExtract"
          class="com.salesforce.dataloader.process.ProcessRunner"
          scope="prototype">
      <description>BatchCustomerAccountExtract</description>
        <property name="name" value="BatchCustomerAccountExtract"/>
        <property name="configOverrideMap">
            <map>
                <entry key="sfdc.debugMessages" value="false"/>
                <entry key="sfdc.debugMessagesFile" value="Logs/"/>
                <entry key="sfdc.endpoint" value="https://login.salesforce.com"/>
                <entry key="sfdc.username" value="[omitted]"/>
                <entry key="process.encryptionKeyFile" value="key.txt"/>
                <entry key="sfdc.password" value="fakeValue"/>
                <entry key="sfdc.entity" value="Account"/>
                <entry key="sfdc.timeoutSecs" value="600"/>
                <entry key="sfdc.extractionRequestSize" value="1000"/>
                <entry key="process.operation" value="extract"/>
                <entry key="dataAccess.type" value="csvWrite"/>
                <entry key="process.enableExtractSuccessOutput" value="true"/>
                <entry key="process.outputError" value="BatchCustomerAccountExtract.csv"/>
                <entry key="dataAccess.name" value=".\Data\BatchContractUpdate.csv"/>
                <entry key="sfdc.extractionSOQL" value="
                SELECT Id, Name, Client_External_ID__c, Customer_Number__c, Account_RecordID__c, Portal_Access_Code__c
                FROM Account 
                WHERE Id IN ('[omitted]')"/>
            </map>
        </property>
    </bean>
</beans>'''

root = ET.fromstring(xml)
print(root.find(".//entry[@key='sfdc.extractionSOQL']").attrib['value'])

输出

SELECT Id, Name, Client_External_ID__c, Customer_Number__c, Account_RecordID__c, Portal_Access_Code__c  FROM Account                  WHERE Id IN ('[omitted]')

相关问题