首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有名称空间的SimpleXMLelement

带有名称空间的SimpleXMLelement
EN

Stack Overflow用户
提问于 2017-01-11 22:12:44
回答 1查看 32关注 0票数 0

我还没有将XML与PHP结合使用,但由于我在SOAP调用后收到了XML,所以我想我必须处理它。

好的。因此,我得到了以下内容:

代码语言:javascript
复制
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/jRecordBroadcastRowWS" xmlns:bcr="http://soap.sforce.com/schemas/class/jRecordUtils" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:body>
        <queryrequestsresponse>
            <result>
                <bcr:customid>REQ16569</bcr:customid>
                <bcr:externalid xsi:nil="true">
                    <bcr:recordid>a035700001CM60kAAD</bcr:recordid>
                    <bcr:requestid>a1J5700000857EYEAY</bcr:requestid>
                </bcr:externalid>
            </result>
            <result>
                <bcr:customid>SRQ100784</bcr:customid>
                <bcr:externalid xsi:nil="true">
                    <bcr:recordid>a033E000001PxfAQAS</bcr:recordid>
                    <bcr:requestid>a1J3E0000000GSaUAM</bcr:requestid>
                </bcr:externalid>
            </result>
        </queryrequestsresponse>
    </soapenv:body>
</soapenv:envelope>

所以我试着拿到recordid的。但是它并没有存储在recordid的

代码语言:javascript
复制
$xmlresponse = new SimpleXMLElement($response); 
$recordid = $xmlresponse->children('soapenv', true)->Body->queryrequestsresponse->result->children('BCR', true)->externalid->recordid;
var_dump($recordid);

只需确保(因为我使用BCR作为大写)以确保正确,我在检索getNamespaces()后执行了vardump,结果如下所示。

代码语言:javascript
复制
array(4) { 
["soapenv"]=> string(41) "http://schemas.xmlsoap.org/soap/envelope/" 
[""]=> string(58) "http://soap.sforce.com/schemas/class/jRecordBroadcastRowWS" 
["BCR"]=> string(49) "http://soap.sforce.com/schemas/class/jRecordUtils" 
["xsi"]=> string(41) "http://www.w3.org/2001/XMLSchema-instance" 
}

那么我该如何解析这些ID呢,我是不是把语法放错了?

EN

回答 1

Stack Overflow用户

发布于 2017-05-18 17:11:29

混合命名空间的SimpleXML很难实现。使用XPath更容易:

代码语言:javascript
复制
foreach ($xmlresponse->xpath('//bcr:recordid') as $sxe) {
    echo (string) $sxe . "\n";
}

输出:

代码语言:javascript
复制
a035700001CM60kAAD
a033E000001PxfAQAS
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41593157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档