0

我尝试从 XML 文件中获取数据。数据如下所示:

<MeasuringPoints>
 <MeasuringPoint ID="Base" LastChange="2013-05-17T09:29:59.293" WeatherCondition="8">
   <Name LngID="IT">Name 1</Name>
   <Name LngID="DE">Name 2</Name>
   <Name LngID="EN>Name 3</Name>
 </MeasuringPoint>
 <MeasuringPoint ID="Middle" LastChange="2012-08-01T11:47:33.160" WeatherCondition="14">
   <Name LngID="IT">Name 1a</Name>
   <Name LngID="DE">Name 2a</Name>
   <Name LngID="EN">Name 3a</Name>
 </MeasuringPoint>
 <MeasuringPoint ID="Top" LastChange="2013-05-17T09:29:59.293" WeatherCondition="8">
   <Name LngID="DE">Name 1b</Name>
   <Name LngID="IT">Name 2b</Name>
   <Name LngID="EN">Name 3b</Name>
 </MeasuringPoint>
</MeasuringPoints>

我也有这个 php 代码:

foreach ($xml->Area as $area) 
{    
   $MEASURING_POINTS = $area->MeasuringPoints->MeasuringPoint['ID'];
   $MEASURING_LAST_CHANGE = $area->MeasuringPoints->MeasuringPoint['LastChange'];

   echo $MEASURING_POINTS;
   echo " - ";
   echo $MEASURING_LAST_CHANGE;
}

该代码有效,但仅适用于第一个MeasuringPointBaseID)。

如何从其他MeasuringPointMiddleTopID)获取数据?

谢谢你的帮助!

阿德里安

4

1 回答 1

0

您需要遍历MeasuringPoint.

试试这个:

foreach ($xml->Area as $area) 
{    
   foreach($area->MeasuringPoints->MeasuringPoint as $point) {
      $MEASURING_POINT = $point['ID'];
      $MEASURING_LAST_CHANGE = $point['LastChange'];

      echo $MEASURING_POINT;
      echo " - ";
      echo $MEASURING_LAST_CHANGE;
   }
}

要仅获得第三个测量点,请尝试以下操作:

foreach ($xml->Area as $area) 
{    
   $point = $area->xpath("/MeasuringPoints/MeasuringPoint[@ID='Top']")
   $MEASURING_POINT = $point['ID'];
   $MEASURING_LAST_CHANGE = $point['LastChange'];

   echo $MEASURING_POINT;
   echo " - ";
   echo $MEASURING_LAST_CHANGE;
}
于 2013-05-17T08:47:29.937 回答