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条)

报告相同问题?