Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Parsing XML and Display in a tableView for Android

So I have been trying to do this for a while. Looking at all the Q&A here in the forum for parsing XML (locally, externally etc) and nothing seems to work for me. Here's my code…

var win = Titanium.UI.currentWindow;

//load local xml file
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'test.xml');
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);

//get data from xml file and store in a list
var custList = doc.documentElement.getElementsByTagName("customer");

//create an array and save the data into array

var custArray = [];

for(var i = 0; i<custList.length;i++)
{
    custArray[i] = custList.item(i).text;
};

var rows = Ti.UI.createTableViewRow({data:custArray});

var tableView = Titanium.UI.createTableView({data:custArray});

win.add(tableView);

And no info appears in the list. My XML is formatted correctly, I've already checked that.
I've seen some people use createTableViewRow with createLabel but it throws an error whenever I use tableViewRow. Any help would be greatly appreciated.

— asked October 26th 2010 by Ben Boblis
  • android
  • createlabel
  • parse
  • tableview
  • tableviewrow
  • xml
1 Comment
  • Update: Figured out you have to keep the info in the for loop:

    var custList = doc.documentElement.getElementsByTagName("customer");
    
    //create an array and save the data into array
    
    var custArray = [];
    var x = 0;
    
    for(var i = 0; i<custList.length;i++)
    {
        var item = custList.item(i);
        var names = item.getElementsByTagName("name").item(0).text;
        var row = Ti.UI.createTableViewRow({height:40, hasChild:true});
        var label = Ti.UI.createLabel({
            text:names,
            color:'black',
            font:{fontSize:18},
            top:5,
            height:'auto',
            textAlign:'left'
        });
        row.add(label);
        row.name = item.getElementsByTagName("name").item(0).text;
        custArray[x++] = row;
    };
    
    — commented November 10th 2010 by Ben Boblis

1 Answer

  • Try replacing:

    var xmltext = file.read().text;

    with:

    var xmltext = file.read().toString();

    — answered October 27th 2010 by Nick Schipano
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.