需要一种使用xmldoc迭代XML的方法

时间:2020-12-11 14:31:26

Using Appery.io javascript server side code. Basically a MongoDB.

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

Libraries being used xmldoc.js 0.1.2 & sax.js 0.5.4

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

Additional libraries available: Underscore.js 1.5.1 moment.js 2.1.0 Handlebars.js 1.0.0

其他可用库:Underscore.js 1.5.1 moment.js 2.1.0 Handlebars.js 1.0.0

Using the code below I am able to access a the first single node value I need and insert that one item into the database but I need to way to continue iterating over the xml and continue creating the objects in the database. I would prefer to send this in 1 API call if possible but I don't think it is. Is there a way to iterate over via a loop or something that will "skip" the previously sent items. There is only about 20 results from the url below.

使用下面的代码,我能够访问我需要的第一个单节点值,并将该项插入数据库,但我需要继续迭代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 个解决方案

#1


0  

I tried the code below and it worked for me:

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

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);
}

Example of what is printed (I am putting just one entry here, the code actually prints several entries like this, one each news)

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

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'

I hope that helps.

我希望有所帮助。

#1


0  

I tried the code below and it worked for me:

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

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);
}

Example of what is printed (I am putting just one entry here, the code actually prints several entries like this, one each news)

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

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'

I hope that helps.

我希望有所帮助。