使用ElementT分析特定项的XML

2024-10-02 18:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在向Salesforce merge API发出请求,得到如下响应:

xml_result = '<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com">
    <soapenv:Header>
        <LimitInfoHeader>
            <limitInfo>
                <current>62303</current>
                <limit>2680000</limit><type>API REQUESTS</type></limitInfo>
        </LimitInfoHeader>
    </soapenv:Header>
    <soapenv:Body>
        <mergeResponse>
            <result>
                <errors>
                    <message>invalid record type</message>  
                    <statusCode>INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY</statusCode>
                </errors>
                <id>003skdjf494244</id>
                <success>false</success>
           </result>
        </mergeResponse>
    </soapenv:Body>
</soapenv:Envelope>'

我希望能够解析这个响应,如果success=false,则返回错误、状态码和消息文本。在

我试过以下方法:

^{pr2}$

…以及findfindtext和{}的其他变体,但我似乎无法让它们返回任何结果。这就是我被困的地方。我尝试过遵循ElementTree文档,但不知道如何解析树中的特定元素。在


Tags: apitypebodyxmlresultcurrentsoapheader
1条回答
网友
1楼 · 发布于 2024-10-02 18:25:41

Element.find() finds the first child with a particular tag https://docs.python.org/2/library/xml.etree.elementtree.html#finding-interesting-elements

由于mergeResponse是子代,而不是子代,因此在这种情况下应该使用XPath语法:

root.find('.//{urn:partner.soap.sforce.com}mergeResponse')

节点将返回。.//搜索从当前节点开始的所有子节点(在本例中是根节点)。在

相关问题 更多 >