PHP Read XML Podcast RSS Feed

社会主义新天地 提交于 2019-12-01 01:52:26
ThW

Nodes have an getAttribute() method. So you can use:

$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')

But here is another and more comfortable way to fetch nodes and values from an XML DOM: Use Xpath. See this answer: https://stackoverflow.com/a/20225186/2265374

The $node->getElementsByTagName('enclosure')->item(0) will result in an error if no element is found (same goes for SimpleXML btw). If the node list is cast to string in Xpath, the result is just an empty string and no error is triggered.

You can directly fetch attributes this way, too. Like the url attribute of the enclosure element:

echo 'Enclosure Url: ', $xpath->evaluate('string(enclosure/@url)', $rssItem), "\n";

Apologies if you're determined to use DOMDocument() in this, but since nobody has posted an answer so far...here's a script which uses simple_xml_load_file(), which I found quite easy to get to grips with.

    <?php

        $rss_array = array('http://rss.computerworld.com/computerworld/s/feed/topic/231', 'http://rss.computerworld.com/computerworld/s/feed/topic/230', 'http://rss.computerworld.com/computerworld/s/feed/topic/66', 'http://www.engadget.com/rss.xml', 'http://feeds.webservice.techradar.com/rss/new', 'http://feeds.arstechnica.com/arstechnica/index', 'http://www.notebookcheck.net/News.152.100.html', 'http://electronista.feedsportal.com/c/34342/f/626172/index.rss', 'http://www.anandtech.com/rss/pipeline/', 'http://www.digitimes.com/rss/daily.xml', 'http://feeds.feedburner.com/TechCrunch/', 'http://feeds2.feedburner.com/ziffdavis/pcmag/breakingnews', 'http://feeds.feedburner.com/Liliputing', 'http://feeds.slashgear.com/slashgear', 'http://feeds.feedburner.com/GizmagEmergingTechnologyMagazine', 'http://www.zdnet.com/news/rss.xml', 'http://feeds.feedburner.com/mobilityupdate', 'http://www.techmeme.com/feed.xml', 'http://www.notebookreview.com/rss.xml'); 

    for ($i=0; $i<count($rss_array); $i++ ) {
                $rssfeed = simplexml_load_file($rss_array[$i]);
                foreach ($rssfeed->channel as $channel) {

                    echo '<h1>' . htmlentities($channel->title) . '</h1>';
                    echo '<p>' . htmlentities($channel->description) . '</p>';
                    echo '<p><a href="' . htmlentities($channel->link) . '">' .
                        htmlentities($channel->link) . '</a></p>';

                    echo '<input type="button" value="  >>>  " onClick="downloadFileViaAjax(\'' . htmlentities($channel->link) . '\')">';     



                    echo '<ul>';
                    foreach ($channel->item as $item) {
                        echo '<li><a href="' . htmlentities($item->link) . '">';
                        echo htmlentities($item->title) . '</a>';
                    //  echo htmlentities($item->description) . '</li>';
                        echo '<input type="button" value="  >>>  " onClick="downloadFileViaAjax(\'' . htmlentities($item->link) . '\')"></li>';   

                    }
                    echo '</ul>';
                }
    }//fur ( $rss_array++    )






    ?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!