簡體   English   中英

無法從具有名稱空間的XML獲取數據

[英]Can't get data from XML with namespaces

我提出了一個肥皂請求,並得到以下答復:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://*************/******/****" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://*************/******/****/***">
  <SOAP-ENV:Body>
    <ns1:GetEventV2Response>
      <ns1:GetEventV2Result>
       <ns1:Events>
         <ns1:Event>
           <ns1:Id>147624</ns1:Id>
           <ns1:Name>Rockstars</ns1:Name>
         <ns1:Genre>
         ...

我試圖在<ns1:Event>內獲取元素ID,但是下面的代碼不起作用,也不知道為什么。 我搜索並嘗試了一些解決方案,但沒有成功。

header("Content-type: text/xml");
  ....
  $response = curl_exec($ch);
  curl_close($ch);
  x = new SimpleXmlElement($response);
  foreach($x->xpath('//ns1:Event') as $event) {
      var_export($event->xpath('ns1:Id'));
  }

使用SimpleXMLElement :: registerXPathNamespace注冊名稱空間。 例如:

$x->registerXPathNamespace('ns1', 'http://*************/******/****');

然后:

$event->registerXPathNamespace('ns1', 'http://*************/******/****');

你為什么不試試這個?

$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//ns1:Event') as $item)
{
    print_r($item);
}

http://php.net/manual/zh/function.simplexml-load-string.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM