Saturday, 10 June 2017

Parse XML from site and get values from elements

I am writing a NodeJS script to get the XML from a website and get the values from a specific element.

However, when running it it says 'undefined'.

Code:

var http = require('http');
var xml2js = require('xml2js');

var parseString = require('xml2js').parseString;

var req = http.get("http://feeds.nos.nl/nosnieuwsvideo?format=xml", function(res) {
  // save the data
  var xml = '';
  res.on('data', function(chunk) {
    xml += chunk;
    console.log(xml);
  });

 res.on('end', function() {
      parseString(xml, function(err, result) {
      console.log(result['title']);
      });
    });
 });

req.on('error', function(err) {
  // debug error
});

What's the problem?

Thanks



via Teun

No comments:

Post a Comment