Вопрос

I am using the following code

var request = require('request');
var cheerio = require('cheerio');
var url = "http://www.mbl.is/feeds/fp/";
request(url, function(err, resp, body) {
    if (err)
        throw err;
    $ = cheerio.load(body,{xmlMode : true});
    $('item').each(function(item, xmlItem){
        console.log($(xmlItem).find('title').text());
        console.log($(xmlItem).find('link').text());
        console.log($(xmlItem).children()[3]['children'][0]['data']);
    });
});

And my problem is, why can't the third line in the .each loop be

console.log($(xmlItem).find('pubDate').text());

If I use that line the output is empty but the structure of the downloaded xml file tells me that should not be the case.

Это было полезно?

Решение

Re-configure the cheerio object adding lowerCaseTags flag;

$ = cheerio.load(body, {
  xmlMode: true,
  lowerCaseTags: true
});

Now console.log($(xmlItem).find('pubDate').text()); should work fine.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top