【问题标题】:php parse soap response with nsphp用ns解析soap响应
【发布时间】:2021-03-24 07:22:42
【问题描述】:

我有以下肥皂反应

<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
    <ns0:READResponse xmlns:ns0="/PELTT01">
      <ns0:st_flag>0</ns0:st_flag>
      <ns0:st_title></ns0:st_title>
      <ns0:pod_date></ns0:pod_date>
      <ns0:pod_time></ns0:pod_time>
      <ns0:pod_name></ns0:pod_name>
      <ns0:web_status>
        <ns0:web_date>date1</ns0:web_date>
        <ns0:web_time>time1</ns0:web_time>
        <ns0:web_station>station1</ns0:web_station>
        <ns0:web_status_title>title1</ns0:web_status_title>
      </ns0:web_status>
      <ns0:web_status>
        <ns0:web_date>date2</ns0:web_date>
        <ns0:web_time>time2</ns0:web_time>
        <ns0:web_station>station2</ns0:web_station>
        <ns0:web_status_title>title2</ns0:web_status_title>
      </ns0:web_status>
      ...
      ...
 <ns0:web_status_counter>5</ns0:web_status_counter>
</ns0:READResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我尝试获取 web_status 节点,但一切都失败了

试试 1

$dom = new DOMDocument();
$dom->load($xml_response);
$web_status  = $dom->getElementsByTagName('web_status');

试试 2

$dom = new DOMDocument();
$dom->load($xml_response);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns0", "/PELTT01");
$web_status = $xpath->query('//ns0:web_status');

谁能帮我解析一下这个soap响应

任何帮助表示赞赏

【问题讨论】:

    标签: soap xml-parsing


    【解决方案1】:

    如果我对您的理解正确,您正在寻找与您的 Try 2 相近的东西:

    $dom->loadXML($xml_response);#note: NOT just "load"
    $xpath = new DOMXPath($dom);
    $xpath->registerNamespace("ns0", "/PELTT01");
    $web_status = $xpath->query('//ns0:web_status');
    
    foreach ($web_status as $wb)
    {
        $cn = $wb->childNodes;
        foreach ($cn as $c){
            echo trim($c->textContent);
            }
    }
    

    输出:

            date1
            time1
            station1
            title1
          
            date2
            time2
            station2
            title2
          
    

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 2014-03-26
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多