在SimpleXML中提取一个名为时间的元素


Extracting an element called time in SimpleXML

我使用file_get_contents()从外部站点获取了一个XML文档。在XML中,有一个"时间"条目。(这不包含当前实时)。以下是显示结构的示例XML文档:

<?xml version="1.0" encoding="utf-8"?>
<weatherdata>
  <location>
    <name>New York</name>
    <type/>
    <country>US</country>
    <timezone/>
    <location altitude="0" latitude="40.714272" longitude="-74.005966" geobase="geonames" geobaseid="0"/>
  </location>
  <credit/>
  <meta>
    <lastupdate/>
    <calctime>0.3297</calctime>
    <nextupdate/>
  </meta>
  <sun rise="2015-02-18T11:45:10" set="2015-02-18T22:34:36"/>
  <forecast>
    <time day="2015-02-18">
      <symbol number="600" name="light snow" var="13d"/>
      <precipitation value="0.35" type="snow"/>
      <windDirection deg="162" code="SSE" name="South-southeast"/>
      <windSpeed mps="3.26" name="Light breeze"/>
      <temperature day="26.26" min="0.19" max="26.26" night="12.31" eve="14.38" morn="0.19"/>
      <pressure unit="hPa" value="1003.66"/>
      <humidity value="60" unit="%"/>
      <clouds value="clear sky" all="0" unit="%"/>
    </time>
    <time day="2015-02-19">
      <symbol number="600" name="light snow" var="13d"/>
      <precipitation value="0.5" type="snow"/>
      <windDirection deg="306" code="NW" name="Northwest"/>
      <windSpeed mps="9.53" name="Fresh Breeze"/>
      <temperature day="16.59" min="2.39" max="16.59" night="2.39" eve="8.37" morn="13.95"/>
      <pressure unit="hPa" value="1000.78"/>
      <humidity value="45" unit="%"/>
      <clouds value="scattered clouds" all="44" unit="%"/>
    </time>
    <time day="2015-02-20">
      <symbol number="800" name="sky is clear" var="01d"/>
      <precipitation/>
      <windDirection deg="308" code="NW" name="Northwest"/>
      <windSpeed mps="10.07" name="Fresh Breeze"/>
      <temperature day="13.5" min="0.48" max="14.29" night="0.48" eve="10.08" morn="1.04"/>
      <pressure unit="hPa" value="1016.68"/>
      <humidity value="41" unit="%"/>
      <clouds value="clear sky" all="0" unit="%"/>
    </time>
    <time day="2015-02-21">
      <symbol number="601" name="snow" var="13d"/>
      <precipitation value="8.75" type="snow"/>
      <windDirection deg="138" code="SE" name="SouthEast"/>
      <windSpeed mps="3.93" name="Gentle Breeze"/>
      <temperature day="17.87" min="-6.41" max="30.97" night="30.97" eve="24.13" morn="-6.41"/>
      <pressure unit="hPa" value="1035.89"/>
      <humidity value="0" unit="%"/>
      <clouds value="broken clouds" all="65" unit="%"/>
    </time>
    <time day="2015-02-22">
      <symbol number="600" name="light snow" var="13d"/>
      <precipitation value="0.05" type="snow"/>
      <windDirection deg="289" code="WNW" name="West-northwest"/>
      <windSpeed mps="12.12" name="Strong breeze"/>
      <temperature day="30.6" min="16.48" max="33.58" night="16.48" eve="23.45" morn="33.58"/>
      <pressure unit="hPa" value="1021.45"/>
      <humidity value="0" unit="%"/>
      <clouds value="clear sky" all="0" unit="%"/>
    </time>
    <time day="2015-02-23">
      <symbol number="600" name="light snow" var="13d"/>
      <precipitation value="0.86" type="snow"/>
      <windDirection deg="297" code="WNW" name="West-northwest"/>
      <windSpeed mps="3.35" name=""/>
      <temperature day="19.4" min="11.91" max="19.4" night="15.69" eve="11.91" morn="13.69"/>
      <pressure unit="hPa" value="1032.5"/>
      <humidity value="0" unit="%"/>
      <clouds value="few clouds" all="20" unit="%"/>
    </time>
    <time day="2015-02-24">
      <symbol number="601" name="snow" var="13d"/>
      <precipitation value="12.36" type="snow"/>
      <windDirection deg="71" code="ENE" name="East-northeast"/>
      <windSpeed mps="8.08" name="Fresh Breeze"/>
      <temperature day="24.73" min="18.99" max="25.95" night="23.5" eve="25.95" morn="18.99"/>
      <pressure unit="hPa" value="1014.9"/>
      <humidity value="0" unit="%"/>
      <clouds value="overcast clouds" all="100" unit="%"/>
    </time>
  </forecast>
</weatherdata>

我试着这样获取时间元素:

$xml->forecast->time[day]

但这会返回当前日期。我想这是因为PHP的time()函数。因此,我不能再像这样处理XML树了:

<?php
$xmlString = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=New York&mode=xml&units=imperial&cnt=7");
$xml = new SimpleXMLElement($xmlString);
$loc = $xml->location->name.", ".$xml->location->country;
$day1 = date('Y-m-d', strtotime('+1 day'));
echo $xml->forecast->time[day1]->symbol['name'];

首先,您需要使用$day1,而不是day1。你错过了$

有了这个,您可以使用xpath来查找相关条目:

$xml->forecast->xpath("//time[@day='$day1']")[0]->symbol['name'];

查看有关xpath()方法的手册页面:http://php.net/manual/en/simplexmlelement.xpath.php并全面了解CCD_ 7。

不太确定我是否理解您想要什么,但以下是如何在xml:中获取所有日期的所有名称

foreach ($xml->forecast->time as $day){
    echo $day->symbol["name"]."<br />";
}

输出:

light snow
light snow
sky is clear
snow
light snow
light snow
snow