douningqiu4991 2013-05-17 08:43 采纳率: 0%
浏览 28
已采纳

从XML文件中获取数据

I try to get data from XML file. The data looks like this:

<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>

I also have this php code:

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

The code is working but only for first MeasuringPoint (the Base ID).

How to get the data from other MeasuringPoint (Middle and Top ID)?

Thanks for any help!

Adrian

  • 写回答

1条回答 默认 最新

  • dongzhang3482 2013-05-17 08:47
    关注

    You need to iteratore over MeasuringPoint.

    Try this:

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

    To only get the third MeasuringPoint try this:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示