douzhan1994 2010-04-21 09:57
浏览 48
已采纳

XML RSS Feed解析PHP

With an XML feed like so:

<w:current temperature="22.2" dewPoint="12.9" humidity="56" windSpeed="5.6" windGusts="9.3" windDirection="ESE" pressure="1017.8" rain="0.0" />

and

<w:forecast day="Thursday" description="Mostly Sunny. Warm." min="17" max="29" icon="2" iconUri="http://www.weather.com.au/images/icons/2.gif" iconAlt="Mostly Sunny" />

How do I parse it in PHP using the dom?

$doc = new DOMDocument();
$doc->load('http://rss.weather.com.au/sa/adelaide');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'description' => $node->getElementsByTagName('w')->item(0)->nodeValue,
        );
    array_push($arrFeeds, $itemRSS);
}

Returns error :Notice: Trying to get property of non-object in /var/www/index.php on line 123

  • 写回答

2条回答 默认 最新

  • doulu3399 2010-04-21 12:02
    关注

    I'd personally use SimpleXML for this, you just have to bear in mind the custom namespace which is being used to carry the weather data, here's a little example (with no error handling etc) which I've just tested and fetches the current weather.

    //Fetch the feed
    $feed = file_get_contents("http://rss.weather.com.au/sa/adelaide");
    //Load it into simplexml
    $weather = simplexml_load_string($feed);
    //Get namespace descendants using the w namespace defined in the feed
    $channelelements = $weather->channel->item->children("http://rss.weather.com.au/w.dtd");
    //Looping through each of the attributes and echoing them, you can do what you want with them at this point
    foreach($channelelements->attributes() as $k => $attr) {
        echo $k.' = '.$attr.'<br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀