Titanium Community Questions & Answer Archive

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

Load data from xml file to create tableview

I need to load data from xml file and use it to create tableview. But my code doesn't work very well and here is my code.

var win = Titanium.UI.currentWindow;
//
//load local xml file
//
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'EducationTitle.xml');
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);
//
//get data from xml file and store in a list
//
var titleList = doc.documentElement.getElementsByTagName("data");

//
//create an array and save the data into array
//
var TVArray = [];

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

var tableview = Titanium.UI.createTableView({
    data:TVArray
});

This is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<Education>
<data>{title:'Courses',hasChild:true,test:'../Submain/Courses.js'},</data>
<data>{title:'Courses',hasChild:true,test:'../Submain/Courses.js'}</data>
</Education>

The problem is the tableview is created, but without title.

Is there anyone can help me solve this problem, thank you.

— asked July 26th 2010 by Haoming Cheng
  • tableview
  • xml
0 Comments

2 Answers

  • Try to put a valid json string there

    <data>{"title":"Courses","hasChild":"true","test":"../Submain/Courses.js"}</data>
    

    and then

    TVArray[i] = JSON.parse(titleList.item(i).text);
    
    — answered July 26th 2010 by Dan Tamas
    permalink
    3 Comments
    • Hi Tamas. I've solved this problem by using table view row. Thank you for your answer.=)

      — commented July 26th 2010 by Haoming Cheng
    • Hi Tamas. I've solved this problem by using table view row. Thank you for your answer.=)

      — commented July 26th 2010 by Haoming Cheng
    • When you say you used table view row, what do you mean? I'm stuck with the same problem and can't figure out how table view row would fix it. Any help would be great. Ben.Boblis@Gmail.com

      — commented October 26th 2010 by Ben Boblis
  • now the error is at line 10 (Script Error = 'undefined' is not an object (evaluating 'result.read().text') at app.js (line 10).)

    var win1 = Titanium.UI.createWindow({
    title:'Tab 1',
    backgroundColor:'#fff'
    });

    var result = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'EducationTitle.xml');
    var xmltext = result.read().text;
    var xml = Ti.XML.parseString(xmltext);

    var name = xml.documentElement.getElementsByTagName("name");
    var value = xml.documentElement.getElementsByTagName("value");
    var data = [];

    for (var i=0;i<params.item.length;i++) {
    Ti.API.log('Param '+i+': Name: '+name.item(i).text);
    Ti.API.log('Param '+i+': Value: '+value.item(i).text);

    // Add to array
    data.push(name.item(i).text);
    

    }

    — answered August 6th 2012 by nicolò monili
    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.