SimpleXML 只想提取特定属性

SimpleXML only want to pull specific attributes

我正在调用一个 API,它有一堆 XML 格式的数据,这里是一堆 XML 数据:

<time from="2016-08-19T15:00:00" to="2016-08-19T18:00:00">
  <symbol number="802" name="scattered clouds" var="03d"/>
  <precipitation/>
  <windDirection deg="196.501" code="SSW" name="South-southwest"/>
  <windSpeed mps="2.16" name="Light breeze"/>
  <temperature unit="celsius" value="28" min="27.75" max="28"/>
  <pressure unit="hPa" value="994.95"/>
  <humidity value="89" unit="%"/>
  <clouds value="scattered clouds" all="32" unit="%"/>
</time>
<time from="2016-08-19T18:00:00" to="2016-08-19T21:00:00">
  <symbol number="500" name="light rain" var="10d"/>
  <precipitation unit="3h" value="2.999" type="rain"/>
  <windDirection deg="235.003" code="SW" name="Southwest"/>
  <windSpeed mps="3.06" name="Light breeze"/>
  <temperature unit="celsius" value="26.06" min="25.87" max="26.06"/>
  <pressure unit="hPa" value="993.86"/>
  <humidity value="88" unit="%"/>
  <clouds value="scattered clouds" all="48" unit="%"/>
</time>

还有很多,但这就是我从 API 请求中得到的结果。我只想从 ="....00:00:00" 中提取 "temperature value" 次,这是一个示例:

<time from="2016-08-20T00:00:00" to="2016-08-20T03:00:00">

我想从XML这个块中提取温度值,我不关心日期是什么我只关心时间是00:00:00。

我试图从一个数组中调用“/00:00:00/”的正则表达式,我将所有数据放入其中,我知道这不是最好的方法,你可以直接从 xml 而不是拉取 xml 然后将其放入数组中,这样做似乎毫无意义!

代码如下:

<!DOCTYPE html>
<html>

<head>
    <title>Open Weather API</title>
</head>

<body>
<?php
    $url3hours = "http://api.openweathermap.org/data/2.5/forecast?q=London,us&mode=xml&appid=Key";

    $xml3hours = simplexml_load_file($url3hours);

    $test[] = $xml3hours->forecast;  

    if (preg_match('/00:00:00/',$test))
    {
        $temp[] = (string) $xml3hours->forecast->time->temperature["value"];
    }

    print_r($temp);
?>


</body>

</html>

抱歉,如果这没有意义,我很不擅长解释东西啊,如果你有任何问题,请问谢谢

使用 Xpath 查找具有 from 属性的 time 标签包含 T00:00:00

$xml3hours = simplexml_load_file($url3hours);

$list = $xml3hours->xpath('//time[contains(@from, "T00:00:00")]/temperature/@value');

foreach($list as $item) {
  echo $item . "\n";
}