gpt4 book ai didi

jquery - 使用 jQuery 读取 RSS 源 - 无法读取 元素

转载 作者:行者123 更新时间:2023-12-01 00:19:27 25 4
gpt4 key购买 nike

我正在使用 jquery 构建网站的 RSS 提要,但链接元素始终返回空白。我自己构建的 javascript 解决方案没有问题,但 jquery 似乎讨厌 link 元素。

下面是 jquery 代码:

function loadData(xml, ifid) {
var htmlStr;
var iframeToWrite = document.getElementById(ifid);

htmlStr = "<html><body>"
var items = $(xml).find("channel item").each(function () {
var $article = $(this);
var title = $article.find("title").text();
var description = $article.find("description").text();
var link = $article.find("link").text();
var pubDate = $article.find("pubDate").text();

htmlStr += "<div class='Rssitem'>\n";
htmlStr += "\t<h3><a href='" + link + "' target='_blank' >" +
title + "</a></h3>\n";
htmlStr += "\t<p>" + description + "</p>\n";
htmlStr += "\t<h6>" + pubDate + "</h6>\n";
htmlStr += "</div>\n"
});

htmlStr += "</body></hmtl>";
iframeToWrite.contentDocument.write(htmlStr);

}

下面是我从我得到的 npr 流中编辑的示例 xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:npr="http://www.npr.org/rss/" xmlns:nprml="http://api.npr.org/nprml" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>News</title>
<link>http://www.npr.org/templates/story/story.php?storyId=1001&amp;ft=1&amp;f=1001</link>
<description>NPR news, audio, and podcasts. Coverage of breaking stories, national and world news, politics, business, science, technology, and extended coverage of major national and world events.</description>
<language>en</language>
<copyright>Copyright 2012 NPR - For Personal Use Only</copyright>
<generator>NPR API RSS Generator 0.94</generator>
<lastBuildDate>Tue, 28 Aug 2012 12:19:00 -0400</lastBuildDate>
<image>
<url>http://media.npr.org/images/npr_news_123x20.gif</url>
<title>News</title>
<link>http://www.npr.org/templates/story/story.php?storyId=1001&amp;ft=1&amp;f=1001</link>
</image>
<item>
<title>Reports: Obama Administration Will Unveil New Fuel-Efficiency Standards</title>
<description>The new rules will require U.S. cars to average 54.5 miles per gallon by 2025.</description>
<pubDate>Tue, 28 Aug 2012 12:19:00 -0400</pubDate>
<link>http://www.npr.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&amp;f=1001</link>
<guid>http://www.npr.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&amp;f=1001</guid>
<content:encoded><![CDATA[<p>The new rules will require U.S. cars to average 54.5 miles per gallon by 2025.</p><p><a href="http://www.npr.org/templates/email/emailAFriend.php?storyId=160172356">&raquo; E-Mail This</a>&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.npr.org%2Ftemplates%2Fstory%2Fstory.php%3FstoryId%3D160172356">&raquo; Add to Del.icio.us</a></p>]]></content:encoded>
</item>
</channel>
</rss>

最佳答案

您必须首先将 xml 字符串解析为 xml 文档 jQuery.parseXML() .
引用文档:

jQuery.parseXML uses the native parsing function of the browser tocreate a valid XML Document. This document can then be passed tojQuery to create a typical jQuery object that can be traversed andmanipulated.

如果您不使用 parseXML 并在其周围包装 jQuery 选择器,jQuery 可能无法正确解释所有节点。

在您的示例中 <link>标签未正确插入且 </link>结束标签已被删除而未解析,因此您无法正确查询链接文本。
您可以确认,如果您执行 console.log($(this))并检查控制台输出的内容。你可以看到<link>然后是文本,但结束语 </link>丢失了。

但是,当首先将 xml 解析为文档对象时,您现在可以在其周围包装 jQuery 选择器并可靠地访问所有节点。当然,假设初始 XML 字符串是有效的 XML。

DEMO - 在 xml 字符串上使用 parseXml 并在之后查询

将此应用到您的代码中将类似于以下内容:

htmlStr = "<html><body>";

// Parse the XML to a document object and then wrap it into a jQuery selector
var $xml = $($.parseXML(xml));

var items = $xml.find("channel item").each(function() {
var $article = $(this);
//..... rest of your code as is

关于jquery - 使用 jQuery 读取 RSS 源 - 无法读取 <link> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12163821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com