2015-05-15 26 views
0

現在我已經打破了這個問題兩天了,所以我希望有人能幫上忙。來自URL的XML multidimentional array php

我需要從網站獲得的XML數據。該代碼返回一個不錯的列表,但缺少時間數組(數組中的數組)。 這裏是我的代碼:

<?php 
$url="http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected="; 
echo $url; 

$sxml = Simplexml_load_file($url); 
var_dump($sxml); 
foreach ($sxml ->ForecastGraphItems ->WindForecastingGraphItem as $type){ 
    echo 'FC '; 
    echo $type ->Forecast."<br>"; 
    echo 'LF '; 
    echo $type ->LoadFactor."<br>"; 
    echo 'DT '; 
    echo $type ->Time[0]->DateTime; 
    echo "<br>"; 
    echo 'BID '; 
    echo $type ->Bid."<br>"; 
    echo 'RA '; 
    echo $type ->RunningAverage."<br>"; 
    echo "<br>"; 
} 
?> 

的XML格式,我得到的時候我只是manualy打開該網站看起來像下面的代碼,或檢查原網站:http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected=

<?xml version="1.0" encoding="UTF-8"?> 

<WindForecastingGraphDataResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Elia.PublicationService.DomainInterface.WindForecasting.v1"> 

    <ErrorMessage i:nil="true"/> 
    <ForecastGraphItems> 
     <WindForecastingGraphItem> 
      <Bid>No</Bid> 
      <Forecast>571.45</Forecast> 
      <LoadFactor>0.32</LoadFactor> 
      <RunningAverage>585.59</RunningAverage> 
      <Time xmlns:a="http://schemas.datacontract.org/2004/07/System"> 
       <a:DateTime>2015-05-12T22:00:00Z</a:DateTime> 
       <a:OffsetMinutes>120</a:OffsetMinutes> 
      </Time> 
     </WindForecastingGraphItem> 
     <WindForecastingGraphItem> 
      <Bid>No</Bid> 
      <Forecast>562.95</Forecast> 
      <LoadFactor>0.32</LoadFactor> 
      <RunningAverage>578.47</RunningAverage> 
      <Time xmlns:a="http://schemas.datacontract.org/2004/07/System"> 
       <a:DateTime>2015-05-12T22:15:00Z</a:DateTime> 
       <a:OffsetMinutes>120</a:OffsetMinutes> 
      </Time> 

現在,當我var_dump($sxml) ,或者如果我嘗試獲取它,則TIME的子數組將被刪除...(請參見下文)。 我也試過foreach ($sxml ->ForecastGraphItems ->WindForecastingGraphItem -> Time as $time){ ...,它工作,但返回空。

任何人都可以幫助爲什麼'時間'數組是空的?

下面回波爲var_dump

http://publications.elia.be/Publications/Publications/WindForecasting.v1.svc/GetForecastGraphDataXml?beginDate=2015-05-13&endDate=2015-05-18&isOffshore=&isEliaConnected=object(SimpleXMLElement)#1 (2){[ 「的ErrorMessage」] =>對象(的SimpleXMLElement)#2(0){} [ 「ForecastGraphItems」] =>對象( SimpleXMLElement#3(1){「WindForecastingGraphItem」] =>數組(481){[0] => object(SimpleXMLElement)#4(5){[「Bid」] => string(2) 「 [」Forecast「] => string(6)」571.45「[」LoadFactor「] => string(4)」0.32「 [」RunningAverage「] => string(6)」585.59「[」Time「] => object(SimpleXMLElement)#485(0){}} [1] => object(SimpleXMLElement)#5(5){[「Bid」] => string(2)「否」 [「Forecast」] => string(6)「562.95」[「LoadFactor」] => string(4 ) 「0.32」 [ 「RunningAverage」] =>串(6) 「578.47」[ 「時間」] => 對象(的SimpleXMLElement)#485(0){}}

+0

這是因爲命名空間'a'在'' – michi

回答

0

正如米尺已經評論你想獲得訪問是在不同的命名空間的元素:

  <Time xmlns:a="http://schemas.datacontract.org/2004/07/System"> 
       <a:DateTime>2015-05-12T22:15:00Z</a:DateTime> 
       <a:OffsetMinutes>120</a:OffsetMinutes> 
      </Time> 

<Time>元素集爲所有它的孩子前綴a到t他XML名字空間http://schemas.datacontract.org/2004/07/System。你感興趣的兩個孩子與前綴:

   <a:DateTime>2015-05-12T22:15:00Z</a:DateTime> 
       <a:OffsetMinutes>120</a:OffsetMinutes> 

你這樣做,告訴你感興趣的,例如孩子的命名空間對於第一個元素:

$type ->Time->children('http://schemas.datacontract.org/2004/07/System')->DateTime; 

就是這樣。

我們糾正一些誤解:

  • 不是一個數組
  • 是,var_dump告訴你們,這是一個,但它只是沒有。
  • var_dump and print_r對於SimpleXMLElement來說並不是特別有用,因爲它們是對它的說明。 SimpleXMLElement::asXML()總是向您顯示具體的XML。
+0

事實上,這是一種孤獨! – dwindey1

+0

Hi @ dwindey1:作爲一名新用戶,請快速瀏覽一下接受答案的工作方式(Stackoverflow投票系統的一部分):[Meta:接受答案如何工作?](http:/ /meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – hakre