需要一种使用xmldoc

时间:2015-10-15 02:04:33

标签: javascript xml node.js mongodb

使用Appery.io javascript服务器端代码。基本上是一个MongoDB。

正在使用的库xmldoc.js 0.1.2& sax.js 0.5.4

可用的附加库:Underscore.js 1.5.1 moment.js 2.1.0 Handlebars.js 1.0.0

使用下面的代码我可以访问我需要的第一个单节点值并将该项插入数据库但我需要继续迭代xml并继续在数据库中创建对象。如果可能的话,我宁愿在1个API调用中发送它,但我认为不是。有没有办法通过循环迭代或“跳过”以前发送的项目。下面的网址只有大约20个结果。

var XHRResponse = XHR2.send("GET",'http://www.bing.com/news?q=Politics&FORM=NSBABR&FORMAT=RSS', {});


var Bingresult = new XmlDocument(XHRResponse.body);

var link = Bingresult.valueWithPath("channel.item.link");

var title = Bingresult.valueWithPath("channel.item.title");

var source = Bingresult.valueWithPath("channel.item.News:Source");

var news_image = Bingresult.valueWithPath("channel.item.News:Image");

var pub_date = Bingresult.valueWithPath("channel.item.pubDate");


//response.success(Bingresult, "Rss"); Same as console.log//


Collection.createObject("55d3d209e4b0d6ee27c97867", "News_", {"link": link,"title": title, "source": source,"news_image": news_image, "pub_date": pub_date})._id;

1 个答案:

答案 0 :(得分:0)

我尝试了下面的代码,它对我有用:

var XHRResponse = XHR2.send("GET",'http://www.bing.com/news?q=Politics&FORM=NSBABR&FORMAT=RSS', {});

var Bingresult = new XmlDocument(XHRResponse.body);

// Pull out the <channel> node
var channel = Bingresult.childNamed("channel");

// Filter out only 'item' children
var items = channel.childrenNamed("item");
console.log("Found %s items.", items.length);

// Loop through each item
items.forEach(function(item) {
   var title = item.valueWithPath("title");         
   var link = item.valueWithPath("link");
   var source = item.valueWithPath("News:Source");
   var news_image = item.valueWithPath("News:Image");
   var pub_date = item.valueWithPath("pubDate");

   // Print values - you can add them to db here       
   console.log("\nTitle '%s' \nLink '%s' \nSource '%s' \nImage '%s' \nDate '%s'", title, link, source, news_image, pub_date);
}

打印内容的示例(我只在这里输入一个条目,代码实际上打印了几个这样的条目,每条新闻一个)

Title 'Bernie Sanders Right at Home in Democratic Party' 
Link 'http://www.bing.com/news/apiclick.aspx?ref=FexRss&aid=&tid=08F7562EF23642259F2CE9F19679C141&url=http%3a%2f%2fwww.ammoland.com%2f2015%2f10%2fbernie-sanders-right-at-home-in-democratic-party%2f&c=_Lbqne5iN09in8HqtFhw6GS0tbEY8KpchXOcT_gmFmA&mkt=en-us' 
Source 'Ammo Land' 
Image 'http://a2.bing4.com/th?id=ON.032A87B845B1305FEC6642A06226A671&pid=News' 
Date 'Fri, 16 Oct 2015 15:59:00 GMT'

我希望有所帮助。