there must be someone out there who can spot my error! I'm parsing an xml feed and running through a loop to get certain items on the page:
www.highlandradio.com/feed/
I am encountering an error on the line where I am setting the variable mp3Test. All I want to do is to set it equal to the url attribute of the enclosure tag (if it exists). This is why I am including the test to check if each item has an enclosure tag, if it does I am setting it to be equal to the url tag but if it doesn't I am setting it to be equal to 'null.mp3' for now. When running through the loop I am getting the correct numbers for a certain amount and then on some iterations of the loop, i crashes. I am using titanium and so am not getting any proper error messages. It simply highlights the line where I set up mp3Test and says to debug the current instruction pointer. I'm going crazy with this. Please help.
var url="http://www.highlandradio.com/feed/";
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
// Data is returned from the blog, start parsing
var doc = this.responseXML.documentElement;
// begin looping through blog posts
var items = doc.getElementsByTagName("item");
var enclosure = doc.getElementsByTagName("enclosure");
console.log("NEWEST Enclosure Length -----------"+enclosure.length);
for (var i = 0; i < items.length; i++) {
var itemsEnclosureLength=items.item(i).getElementsByTagName("enclosure").length;
var itemsEnclosureExists=items.item(i).getElementsByTagName("enclosure");
console.log("EXISTs ----------------------------------"+ itemsEnclosureExists);
console.log("Newest ENCLOSURE LENGTH ---------------------- [ "+i+ " ]" + itemsEnclosureLength);
var str=items.item(i).getElementsByTagName("content:encoded").item(0).text;
console.log("STRING TEST "+ str);
var patt1=/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i;
var urlMatch = str.match(patt1);
console.log("MATCHES TESTER -----------------------------------" + typeof urlMatch);
data.push({
title: items.item(i).getElementsByTagName("title").item(0).text,
leftImage: str.match(patt1) !== null ? str.match(patt1)[0] : 'image_news.png',
dataToPass: items.item(i).getElementsByTagName("description").item(0).text,
mp3Test: itemsEnclosureLength > 0 ? items.item(i).getElementsByTagName("enclosure").item(i).getAttribute("url"):'NULL.MP3'
});
console.log("Check this --------------------------"+ mp3Test);
}
BUMP2
The problem was that I was using two counters which had the same value! Silly mistake but...Here's the solution:
var itemsEnclosureLength=items.item(i).getElementsByTagName("enclosure").length;
var itemsEnclosureExists=items.item(i).getElementsByTagName("enclosure");
if (itemsEnclosureExists){
for (n=0; n<itemsEnclosureLength; n++){
mp3=items.item(i).getElementsByTagName("enclosure").item(n).getAttribute("url");
}
}
else{
mp3= 'NULL.mp3';
}