开发者

Javascript collapsing ATOM nodes when parsing

开发者 https://www.devze.com 2023-02-10 03:10 出处:网络
I am reading an ATOM feed with jQuery and looping over the results. While doing this jQuery seems to be collapsing two nodes into one.

I am reading an ATOM feed with jQuery and looping over the results. While doing this jQuery seems to be collapsing two nodes into one.

ATOM:

   <link rel="alternate" type="text/html" href="http://twitter.com/leesalove/statuses/34751066604044288" />
   <link rel="image" type="image/png" href="http://a1.twimg.com/profile_images/1236597389    ge_normal.jpg" />

BECOMES

, link : 'http://twitter.com/leesalove/statuses/34751066604044288'

So the second value is lost. Problem I, that's the one I need.

Here is how I am loading the data. Having to use the Google Feed Proxy because it's coming from a different domain then the host.

    var url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=' + escape('http://vivaglam.tidytweet.com/VivaGlam.atom');

    jQuery.getJSON(url, function(data){
        if (data.responseStatus == 200) {
            jQuery(data.responseData.feed.entries).开发者_开发知识库each(function(i,p) {
                console.log(p);
            });
        }
    });


Not sure how you read the contents (which might be were the problem is, but without a sample of your code we cannot tell), but you can use JSONP instead of atom which is javascript friendly :)

var url = 'http://search.twitter.com/search.json?q=from%3Aleesalove&rpp=100&callback=?';

$.getJSON(url, function(data){
    // do what you want with results here..
    // data.total is the count
    // data.results is a list of the results
});

the documentation for the api is at http://search.twitter.com/api/


Update

I see, you are using the google feed api.
If you stare at the specification they show (with the lack of [ ]) that it returns a single value.

And i believe the one they pick is not based on index (the first) but the one with rel="alternate" since that is supposed to give you the link to the article..

You can use the xml version (also returned through json) which returns all elements.
To do this you need to add to your url another parameter &output=xmldoc.

You can use jQuery to read the xml and traverse it.

   var url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&output=xml&q=' + escape('http://vivaglam.tidytweet.com/VivaGlam.atom');

    jQuery.getJSON(url, function(data){
        if (data.responseStatus == 200) {
            var xmlDoc = $.parseXML(data.responseData.xmlString);
            var $xml = $(xmlDoc);

            $xml.find('entry').each(function(i,p) {
                console.log( $(p).find('link[rel="image"]').attr('href') );
            });
        }
    });

Demo at : http://www.jsfiddle.net/gaby/vANcv/
(uses jquery 1.5 for the $.parseXMLdoc, but it would be the same if you just $(xmlstring))

0

精彩评论

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

关注公众号