我尝试从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;
}
代码正在运行,但仅适用于第一个MeasuringPoint
(Base
ID)。
如何从其他MeasuringPoint
(Middle
和Top
ID获取数据?
感谢您的帮助!
阿德里安
答案 0 :(得分: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;
}