尝试从 Yahoo Weather XML 中提取元素 [英] Trying to pull in elements from Yahoo Weather XML

查看:46
本文介绍了尝试从 Yahoo Weather XML 中提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从 Yahoo XML 文档中的 [yweather:astronomy] 的日出元素中提取值.尝试了以下各种组合:

Trying to pull a value from the sunrise element from [yweather:astronomy] in the Yahoo XML Doc. Tried various combinations along the lines of:

echo $yweather->astronomy == 'sunrise';

从日出元素中提取值是正确的术语吗?努力在网络上寻找使用此术语的帮助方式.

Is pulling a value from the sunrise element the right terminology? Struggling to find much in the way of help using this terminology on the web.

其余代码按我的意愿运行

The remainder of the code is functioning as I wish

Yahoo XML 文档 - 片段

Yahoo XML Doc - snippet

<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" 
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>Yahoo! Weather - New York, NY</title>
    <link>
      http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html
    </link>
    <description>Yahoo! Weather for New York, NY</description>
    <language>en-us</language>
    <lastBuildDate>Mon, 12 Dec 2011 1:50 pm EST</lastBuildDate>
    <ttl>60</ttl>
    <yweather:location city="New York" region="NY" country="US"/>
    <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
    <yweather:wind chill="40" direction="0" speed="5"/>
    <yweather:atmosphere humidity="37" visibility="10" pressure="30.54" rising="2"/>
    <yweather:astronomy sunrise="7:09 am" sunset="4:26 pm"/>

rssweather.php

rssweather.php

<?php

// Get XML data from source
$feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f");

// Check to ensure the feed exists
if(!$feed){
die('Weather not found! Check feed URL');
}

$xml = new SimpleXmlElement($feed);

foreach ($xml->channel as $entry){
    echo "<strong>Description</strong> "; 
    echo $entry->description; 
    echo "<br /><strong>Collected on</strong> ";
    echo $entry->lastBuildDate;

    //Use that namespace
    $yweather = $entry->children("http://xml.weather.yahoo.com/ns/rss/1.0");
    echo "<br /><strong>Sunrise</strong> ";
    echo $yweather->astronomy == 'sunrise';

}
>?

推荐答案

我的最终解决方案

// Get and return sunrise time 
function get_sunrise_time(SimpleXMLElement $xml) {
    $weather['sunrise'] = $xml->channel->children('yweather', TRUE)->astronomy[1]->attributes()->sunrise;

    return $weather;
}

这篇关于尝试从 Yahoo Weather XML 中提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆