开发者

Extracting information from XML with jQuery

开发者 https://www.devze.com 2023-02-10 18:24 出处:网络
I can\'t get the link tag to come through when I\'m parsing XML.I\'ve tried several approaches. This is what I\'m using now:

I can't get the link tag to come through when I'm parsing XML. I've tried several approaches. This is what I'm using now:

$(document).ready(function() {  

        $('#news-feed').each(function(){
         var $container = $(this);
         $container.empty();             
            $.get('blogs.xml', function(data){
  开发者_Python百科       $('entry', data).each(function() {
          var $link = $('<a></a>')
          .attr('href', $('link', this).text())
          .text ($('title', this).text());
          var $headline = $('<h5></h5>').append($link);           
          var author = $(this).find('author').text();
          var pubDate = new Date(
            $('published', this).text());
            var pubMonth = pubDate.getMonth() + 1;
            var pubDay = pubDate.getDate();
            var pubYear = pubDate.getFullYear();
            var $publication = $('<div></div>')
                .addClass('publication-date')
                .text('Posted by ' + author + ' on ' + pubMonth + '/' + pubDay +'/'
                +pubYear);

         $('<div></div>')
           .append($headline, $publication)
           .appendTo($container);

            }); 
});

    });

});




<?xml version="1.0" encoding="utf-8"?>
<feed
<entry>
        <title>Quality Aspects of Flexibility and Scalability in Distance Education Design</title>
        <link rel="alternate" type="text/html" href="http://www.personal.psu.edu/author/blogs/instructional_design_and_higher_education/2010/09/quality-aspects-of-flexibility-vs-scalability-in-distance-education-design.html" />
        <id>tag:www.personal.psu.edu,2010:/author/blogs/instructional_design_and_higher_education//16140.270413</id>

        <published>2010-09-02T13:21:31Z</published>
        <updated>2010-09-16T12:49:51Z</updated>
        <author>
            <name>author</name>

        </author>

        <category term="3204" label="distance education" scheme="http://www.sixapart.com/ns/types#tag" /><category term="59026" label="flexible design" scheme="http://www.sixapart.com/ns/types#tag" /><category term="48647" label="wcldportfoliopilot" scheme="http://www.sixapart.com/ns/types#tag" /><category term="54688" label="wcldwebsite" scheme="http://www.sixapart.com/ns/types#tag" />
        <content type="html" xml:lang="en" xml:base="http://www.personal.psu.edu/author/blogs/instructional_design_and_higher_education/">



        </content>
    </entry>
</feed>


You are retrieving the .text() of the link, but the link tag is self-closing so it has not text.

use $('link', this).attr('href') to extract the url from the href attribute of the link tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消