2014-07-14 69 views
0

我一直在使用Bing Maps API來從某個區域獲取流量事件。這將創建一個XML文檔,我可以訪問單個節點的對象,但無法回顯嵌套在其他節點中的任何節點。 XML文檔的樣子:來自有重複節點的XML文檔的PHP回顯對象

<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"> 
<Copyright> 
- Message - 
</Copyright> 
<BrandLogoUri> 
    - Logo - 
</BrandLogoUri> 
<StatusCode>200</StatusCode> 
<StatusDescription>OK</StatusDescription> 
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode> 
<TraceId> 
    - Trace ID- 
</TraceId> 
<ResourceSets> 
<ResourceSet> 
<EstimatedTotal>1</EstimatedTotal> 
<Resources> 
<TrafficIncident> 
<Point> 
<Latitude> - Latitude - </Latitude> 
<Longitude>- Longitude -</Longitude> 
</Point> 
<IncidentId>- IncidentId -</IncidentId> 
<LastModifiedUTC>- Time -</LastModifiedUTC> 
<StartTimeUTC>- Time -</StartTimeUTC> 
<EndTimeUTC>- Time -</EndTimeUTC> 
<Type>- Type -</Type> 
<Severity>- Severity -</Severity> 
<Verified>- True -</Verified> 
<RoadClosed>- True -</RoadClosed> 
<Description> 
    - Description - 
</Description> 
<ToPoint> 
<Latitude>-Latitude -</Latitude> 
<Longitude>- Longitude -</Longitude> 
</ToPoint> 
</TrafficIncident> 
</Resources> 
</ResourceSet> 
</ResourceSets> 
</Response> 

爲了呼應在單個節點的對象我一直在使用下面的PHP代碼:

<?php 
    $xml = simplexml_load_file("link"); 
    echo "Trace Id <br>"; 
    echo $xml->TraceId . "<br>"; 
    echo $xml->Copyright . "<br>"; 
    echo $xml->Verified . "<br>"; 
?> 

呼應緯度這是我已經嘗試了嵌套節點的PHP代碼:

foreach($xml->TrafficIncident->Point->Latitude as $row){ 
     echo'<td>'.$row.'</td>'; 
    } 

但到目前爲止沒有運氣。有任何想法嗎?

+0

什麼是你的錯誤信息? – ToBe

+0

我認爲你的XML路徑是錯誤的,很難說沒有代碼格式。 – ToBe

回答

0

試試這個:

foreach($xml->ResourceSets->ResourceSet->Resources->TrafficIncident as $trafficIncident) 
{ 
    echo $trafficIncident->Point->Latitude.' '.$trafficIncident->Point->Longitude; 
} 
+0

非常感謝,完美工作! :d – user2047296