【问题标题】:How i can get sessionid from this SOAPXML我如何从这个 SOAPXML 中获取 sessionid
【发布时间】:2018-12-22 14:53:13
【问题描述】:

我想从这段 XML 代码中获取 sessionid:

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:header>
     <soapenv:body>
       <p725:loginresponse xmlns:p725="http://www.fleetboard.com/data">
         <p725:loginresponse sessionid="0001nABbah-I8f75oDrVbHrBgOv:s96fb0a4m3"></p725:loginresponse>
        </p725:loginresponse>
     </soapenv:body>
   </soapenv:header>
 </soapenv:envelope>

我试过了,但这不起作用:

$soap=simplexml_load_string($result);
$xml_response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body()->children()->p725;
echo $session_id =  (int) $xml_response->session_id;

【问题讨论】:

    标签: php xml soap xml-parsing


    【解决方案1】:

    有两种方法可以做到这一点。第一个是您当前正在执行的操作,但这涉及命名空间的各种更改,这意味着您需要继续获取正确的子元素和属性本身...

    $soap=simplexml_load_string($result);
    $xml_response = $soap->children("http://schemas.xmlsoap.org/soap/envelope/")->header->body;
    $session_id = $xml_response->children("http://www.fleetboard.com/data")->loginresponse->loginresponse;
    echo $session_id->attributes()->sessionid.PHP_EOL;
    

    或者您可以使用 XPath,您需要先在文档中注册名称空间,然后选择带有 sessionid 元素的 loginresponse 元素。这将返回一个匹配列表,因此您必须使用 [0] 获取第一个匹配项...

    $soap=simplexml_load_string($result);
    $soap->registerXPathNamespace("p725", "http://www.fleetboard.com/data");
    $session_id = $soap->xpath("//p725:loginresponse/@sessionid");
    echo $session_id[0];
    

    【讨论】:

    • 不确定这意味着什么 - 您是否收到任何错误或任何数据?
    • 不,我正在获取空白数据,我需要从此 xml 获取 sessionid
    • 使用您在问题中提供的示例 XML 以及上面的任一代码集输出 0001nABbah-I8f75oDrVbHrBgOv:s96fb0a4m3
    猜你喜欢
    • 2013-05-30
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多