Search code examples
javascriptxmltitaniumtableviewtitanium-mobile

XML data only show some of it


So I am trying to get data from an XML file, but when i loop thru it and want to get out the "countyname" I only get the first one and then it stops. Is the XMl wrong? because I tried with other XML and that works fine with my code that gets the data into my tableview. Best regards! Filip

<?xml version="1.0" encoding="UTF-8"?>
<food_company>
<county>
    <countyname>New York</countyname>
    <city>
    <cityname>New York City</cityname>
        <restaurant>
            <name>Dinos pizzeria</name>
            <phone>01111111</phone>
            <location>broadway1</location>
        </restaurant>
        <restaurant>
            <name>Dinos pizzeria2</name>
            <phone>01111111</phone>
            <location>broadway2</location>
        </restaurant>
        <restaurant>
            <name>Dinos pizzeria3</name>
            <phone>01111111</phone>
            <location>broadway3</location>
        </restaurant>
    </city>
      <countyname>Baldwin County</countyname>
    <city>
    <cityname>Bay Minette</cityname>
        <restaurant>
            <name>Dinos pizzeria</name>
            <phone>01111111</phone>
            <location>broadway1</location>
        </restaurant>
        <restaurant>
            <name>Dinos pizzeria2</name>
            <phone>01111111</phone>
            <location>broadway2</location>
        </restaurant>
        <restaurant>
            <name>Dinos pizzeria3</name>
            <phone>01111111</phone>
            <location>broadway3</location>
        </restaurant>
    </city>
</lan>
</food_company>

app.js Code:

Titanium.UI.setBackgroundColor('#E1E6EE');



// create base UI tab and root window
var win1 = Titanium.UI.createWindow({  


statusBarStyle: Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
tintColor: '#FFF',
backgroundColor:'#E1E6EE',
url: 'lan.js',
tabBarHidden: true,
navBarHidden: true
});

win1.open();

county.js Code:

Ti.include('app_functions.js');

var win = Titanium.UI.currentWindow;

// create a table to display news feeds--------------------------------
var itemsTable = Ti.UI.createTableView({
top : '11%',
left : 0,
leftImage : 'taxi.png',
backgroundColor : '#DCEEDC', //E1E6EE
bottom : '0%',
// search : searchBar,
filterAttribute : 'searchFilter'
});
win.add(itemsTable);

// define xmlFeed (you can customize this with any RSS feed)
var xmlFeed = 'http://eventverket.nu/test/test5.xml';
//'http://83.254.164.137:1000/test.xml';

// create a new HTTP client object
var xhr = Ti.Network.createHTTPClient();

// this method will process the remote data
xhr.onload = function() {

// create an xml object
var xml = this.responseXML;

// create an array that will store news items for our tableView
var data = [];
var data = [];
var items = xml.documentElement.getElementsByTagName("county");
for (var i=0; i<items.length; i++) {
var row = Ti.UI.createTableViewRow({
    title: items.item(i).getTextContent()
}); 
data.push(row); 
}
itemsTable.data = data;

// when the user clicks on a row
itemsTable.addEventListener('click', function(e) {

// NEW WINDOW
var newWindow = Titanium.UI.createWindow({
    backgroundColor : '#DCEEDC', //E1E6EE
    statusBarStyle : Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
    font : fonts[16]['normal'],
    url : "stad.js",
    //backButtonTitle: 'Back',
    //title: e.source.title,
    tabBarHidden : true,
    navBarHidden : true,
    tintColor : '#FFF'
    });

    newWindow.open();
   });

};

// this method will be called if there is an error in accessing the     data
xhr.onerror = function() {
    // hide activity indicator
activityIndicator.hide();

// display error
alert(this.status + ': ' + this.statusText);
return false;
};

// open the remote feed
xhr.open('GET', xmlFeed);

// execute the call to the remote feed
xhr.send();

city.js Code:

Ti.include('app_functions.js');

var newWin = Titanium.UI.currentWindow;



// create a table to display news feeds--------------------------------
var itemsTable = Ti.UI.createTableView({
top : '11%',
left : 0,
leftImage : 'taxi.png',
backgroundColor : '#DCEEDC', //E1E6EE
bottom : '0%',
// search : searchBar,
filterAttribute : 'searchFilter'
});
win.add(itemsTable);

// define xmlFeed (you can customize this with any RSS feed)
var xmlFeed = 'http://eventverket.nu/test/test5.xml';
//'http://83.254.164.137:1000/test.xml';

// create a new HTTP client object
var xhr = Ti.Network.createHTTPClient();

// this method will process the remote data
xhr.onload = function() {

// create an xml object
var xml = this.responseXML;

// create an array that will store news items for our tableView

var data = [];



var items = xml.documentElement.getElementsByTagName("city");
for (var i=0; i<items.length; i++) {
var row = Ti.UI.createTableViewRow({
    title: items.item(i).getTextContent() //
}); 
data.push(row); 
}
itemsTable.data = data;





// when the user clicks on a row
itemsTable.addEventListener('click', function(e) {

// NEW WINDOW
var newWindow = Titanium.UI.createWindow({
    backgroundColor : '#DCEEDC', //E1E6EE
    statusBarStyle : Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
    font : fonts[16]['normal'],
    url : "stad.js",
    //backButtonTitle: 'Back',
    //title: e.source.title,
    tabBarHidden : true,
    navBarHidden : true,
    tintColor : '#FFF'
    });


 });

};

// this method will be called if there is an error in accessing the data
xhr.onerror = function() {
// hide activity indicator
activityIndicator.hide();

// display error
alert(this.status + ': ' + this.statusText);
return false;
};

// open the remote feed
xhr.open('GET', xmlFeed);

// execute the call to the remote feed
xhr.send();

Solution

  • The logic of your code is wrong. Let me explain why... I'm going to comment your code and at the end you will understand what is wrong with it.

    var items = xml.documentElement.getElementsByTagName("county");
    

    With this line of code you're getting all "county" elements of the XML file. In your case there is only one element. So items is a Node.List which contains just one element.

    for (var i=0; i<items.length; i++) {
        ... 
    }
    

    With the for statement you're iterating through all the elements in items. In other words the content of the for statement will be repeated items.lenght times. But items contains only one element! So there will be no iterations.

    Within your statement you're creating new rows. But only one row is going to be created, because of the fact that there are no itertions. For this reason you get only the first "countyname" tag.

    I hope you've understood your error... Now i give to you a simple solution for your problem:

    var data = [];
    var items = xml.documentElement.getElementsByTagName("countyname");
    for (var i=0; i<items.length; i++) {
        var row = Ti.UI.createTableViewRow({
            title: items.item(i).getTextContent()
        }); 
        data.push(row); 
    }
    itemsTable.data = data;
    

    My code just get a list of all the elements whose tag name is "countyname". According to your XML file items will be a Node.List with two elements. Then with a for statement it's possible to create new rows from the textContent of every node of the list!