简体   繁体   English

如何在HTML中显示XML RSS feed?

[英]How to display XML RSS feeds in html?

I've been doing countless hours of research on RSS feeds and I have found nothing that will suit my needs. 我一直在做无数小时的RSS提要研究,但没有发现任何适合我的需求。 I have been working on a site to basically hone my skills as an apprentice programmer and one thing I need to implement is a news feed. 我一直在一个网站上工作,以基本上磨练我作为学徒程序员的技能,而我需要实现的一件事是新闻提要。

How can I create a news feed that can be customized, as in how it is displayed on a webpage? 如何创建可以自定义的新闻提要,就像它在网页上的显示方式一样? What I want to do specifically is have 3 columns, with 3 news posts per row, and each post being displayed in chronological order. 我要专门做的是有3列,每行3个新闻帖子,每个帖子按时间顺序显示。 I was thinking that this could be done with an html table, where I can use javascript to take the data from the XML file and put it into the table, however I could not come up with a solution that would do this, which is the reason why I am here. 我当时以为可以使用html表来完成此操作,可以在其中使用javascript从XML文件中获取数据并将其放入表中,但是我无法提出一种解决方案,那就是我在这里的原因。

I do not expect anyone to do this for me, I just want to know if anyone has either done something like this before, or if there is a simpler way to do this, or if I am not doing this correctly at all. 我不希望有人为我这样做,我只是想知道是否有人以前曾经做过这样的事情,或者是否有更简单的方法来做到这一点,或者我是否根本没有正确做到这一点。

I do not want to use an RSS service that requires me to pay a subscription, or that does not allow me to fully customize the feed, in case anyone gives me a link to one (such as rssinclude) 我不想使用要求我支付订阅费或不允许我完全自定义提要的RSS服务,以防万一有人给我链接到该提要的链接(例如rssinclude)


Solved 解决了

Figured this out a while back, I built my own xml parser starting with javascript, but moved to php when I needed a different method to call rss feeds from different domains. 弄清楚这一点,我从javascript开始构建了自己的xml解析器,但是当我需要一种不同的方法来调用来自不同域的rss feed时,就转移到了php。 Here is the bit where I parsed the passed xml, there is a lot more code that is unrelated to the topic so there are some random variables in there: 这是我解析传递的xml的地方,还有许多与该主题无关的代码,因此其中包含一些随机变量:

$content = file_get_contents($cache);
$x = new SimpleXmlElement($content);
$item = $x->channel->item;
echo '<tr>';
for($i = $xmn; $i < $xmx; $i++) {
    echo '<td class="item"><p class="title clear">' . 
        $item[$i]->title . 
        '</p><p class="desc">' . 
        $desc=substr($item[$i]->description, 0, 250) . 
        '... <a href="' . 
        $item[$i]->link . 
        '" target="_blank">more</a></p><p class="date">' . 
        $item[$i]->pubDate . 
        '</p></td>';
}
echo '</tr>';

And here is the bit where I request the php file and take the resultant text to place it in my html: 这是我请求php文件并将结果文本放入我的html中的位:

function ajax(url, xmn){
        var dataString = [url, xmn],
            jsonString = JSON.stringify(dataString);
            request = $.ajax({
                beforeSend: function(){
                    if(request != null){
                        request.abort();

                    }
                },
                type: "POST",
                url: "/scripts/feed.php",
                data: {jsonString : jsonString}, 
                cache: false,
                success: function(result){
                    document.getElementById('rss').innerHTML = result;
                },
            });

您所需要做的就是为各种输出格式创建XSL样式表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM