dongsang6899 2016-07-26 09:02
浏览 72
已采纳

PHP - 无法读取XML节点值

I am getting below SOAP API response and trying to read XML node value but not getting resultcode & message values.

$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;

What I am doing wrong. Any Idea.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongzi5673 2016-07-26 09:17
    关注

    You'll probably kick yourself, but the tag names are case-sensitive. You just need to change

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

    to:

    $tns = $soapenv->body->children("http://example.com/");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?
  • ¥15 如何用QDomDocument读取内容为空格的xml数据
  • ¥15 请阅读下面代码,帮我修改下代码
  • ¥15 关于#microsoft#的问题:电脑启动后不显示桌面图标和窗口,除任务栏外无法操作任何东西