带名称空间的SimpleXMLelement

时间:2017-01-11 14:12:44

标签: php xml

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

好的。所以我回过头来看看:

<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的

$xmlresponse = new SimpleXMLElement($response); 
$recordid = $xmlresponse->children('soapenv', true)->Body->queryrequestsresponse->result->children('BCR', true)->externalid->recordid;
var_dump($recordid);

请确保(因为我使用BCR作为大写)以确保我做对了,我在检索到getNamespaces()之后做了一个vardump,结果如下:

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?我的语法错了吗?

1 个答案:

答案 0 :(得分:0)

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

foreach ($xmlresponse->xpath('//bcr:recordid') as $sxe) {
    echo (string) $sxe . "\n";
}

输出:

a035700001CM60kAAD
a033E000001PxfAQAS