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.
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);
-
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);
}