【问题标题】:PHP echo objects from an XML document where there are duplicate nodes来自存在重复节点的 XML 文档的 PHP 回显对象
【发布时间】:2014-07-14 22:31:44
【问题描述】:

我一直在使用Bing Maps API 来获取某个区域的交通事件。然后,这将创建一个 XML 文档,我可以访问作为单个节点的对象,但不能回显嵌套在其他节点中的任何节点。 XML 文档如下所示:

<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>';
    }

但到目前为止还没有运气。有什么想法吗?

【问题讨论】:

  • 你的错误信息是什么?
  • 我认为你的 XML 路径是错误的,但如果没有代码格式很难说。

标签: php xml


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多