4

jqueryを使用して自分のWebサイトのRSSフィードを作成していますが、link要素が空白を返し続けます。自分で作成した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>
4

1 に答える 1

4

最初にxml文字列をxmlドキュメントに解析する必要がありますjQuery.parseXML()
ドキュメントを引用するには:

jQuery.parseXMLは、ブラウザのネイティブ解析機能を使用して、有効なXMLドキュメントを作成します。次に、このドキュメントをjQueryに渡して、トラバースおよび操作できる一般的なjQueryオブジェクトを作成できます。

parseXMLを使用せず、jQueryセレクターをラップすると、jQueryがすべてのノードを適切にインターペットできない場合があります。

あなたの例では、<link>タグは正しく挿入されておらず、</link>終了タグは解析せずに削除されているため、リンクテキストを正しくクエリすることはできません。を実行すると、コンソール出力の内容を確認
できます。console.log($(this))thenテキストを見ることができ<link>ますが、クロージング</link>がありません。

ただし、最初にxmlをドキュメントオブジェクトに解析するときに、jQueryセレクターをラップして、すべてのノードに確実にアクセスできるようになりました。もちろん、最初のXML文字列が有効なXMLであると仮定します。

デモ-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
于 2012-08-28T17:34:21.060 に答える