duandai6373 2015-05-02 20:55
浏览 52
已采纳

从stackoverflow.com解析rss时出错。 在PHP中使用SimpleXML

I was trying to parse the rss of the tag PHP, from http://stackoverflow.com and tried to use something other than DOM Model, So I looked into SimpleXML. THis is my code:

<?php
    error_reporting(-1);
    $xml = file_get_contents('https://stackoverflow.com/feeds/tag/php');
    $loaded = simplexml_load_string($xml) or die("There is a problem");
    $str1 = $loaded["entry"]["entry"][0]->title;
    echo $str1;
?>

But nothing is displayed on the screen, and also no error is displayed! The sample data from https://stackoverflow.com/feeds/tag/php can be found at http://gourabt2.cloudapp.net/sample-data/sample_data.xml

Any Help would be very much appreciated! Thanks! Cheers!

  • 写回答

3条回答 默认 最新

  • dpy33121 2015-05-02 21:07
    关注

    You use array-access in SimpleXML to access attributes so:

    $loaded["entry"]
    

    returns the attribute named "entry" from the document element.

    use arrow-access to get the element named "entry" instead:

    $loaded->entry
    

    this returns the element named "entry".

    Additionally take care with namespaces. Parsing a feed with SimpleXML has been outlined already in existing Q&A material, please relate to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效