首页 > 解决方案 > php用ns解析soap响应

问题描述

我有以下肥皂反应

<?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');

谁能帮我解析这个肥皂响应

任何帮助表示赞赏

标签: soapxml-parsing

解决方案


如果我对您的理解正确,您正在寻找与 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
      

推荐阅读