[SOLVED] Parsing XML from remote URL


#1

I’ve been trying to parse a remote XML URL, basically just something totally plain like this:
http://www.npr.org/rss/podcast.php?id=510307

I use HTTP.get() to make the request and I’m trying to parse the XML into a client-side collection. Any pointers on how to do this? I’m using XML2Js and it looks like this:

Meteor.publish('episodeSearch', function() {
	var self = this;

	try {
		var response = HTTP.get("http://www.npr.org/rss/podcast.php?id=510307");

		// parse
		xml2js.parseString(response, function (err, result) {
			console.log(result);
		});
	}

	catch(error) {
		console.log(error);
	}
});

And it just keeps returning undefined.


#2

Is the GET request actually returning a response?


#3

Yeah it is. For now I just ended up using Google’s Feed API to convert it to JSON but would love to be able to properly parse the XML.


Return posts find into meteor publish?
#4

You probably want to use:

xml2js.parseString(response.content, function (err, result) {
  console.log(result);
});

At least, that’s the gist of what I’m using in one of my apps (I’m actually using the synchronous version):

var result = xml2js.parseStringSync(response.content);
// do stuff with results

Which works great.


#5

Yup, the answer is that you’re passing the whole response object instead of its content property value, which is where the response’s content actually resides. That’s the difference in the code in the answer above!