PHP - 无法读取XML节点值

时间:2016-07-26 09:02:55

标签: php xml soap

我正在接受SOAP API响应并尝试读取XML节点值但未获取resultcode& message值。

$result = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <tic:reservationresponse xmlns:tic="http://example.com/">
            <tic:reservationresp>
                <tic:resultcode>-204</tic:resultcode>
                <tic:message>Invalid Location Id</tic:message>
            </tic:reservationresp>
        </tic:reservationresponse>
    </soap:body>
</soapenv:envelope>';

$xml     = simplexml_load_string($result);
$soapenv = $xml->children("http://schemas.xmlsoap.org/soap/envelope/");
$tns     = $soapenv->Body->children("http://example.com/");

echo $tns->reservationresponse->reservationresp->resultcode;

我做错了什么。任何想法。

感谢。

1 个答案:

答案 0 :(得分:0)

你可能会踢自己,但标签名称区分大小写。你只需要改变

$tns = $soapenv->Body->children("http://example.com/");

为:

$tns = $soapenv->body->children("http://example.com/");