I am having the following .xml which i used in my android project.now,am trying to create the same in iPhone using titanium.Can some one tell me how to get this values from the xml file and show them in a table view.? if i give MainCategories the 3 values in it should appear in the table view.Also can some one tell me is there any other better option to achieve this?.thank you.
<string-array name="MainCategories"> <item>Acceleration</item> <item>Angle</item> <item>Area</item> </string-array> <string-array name="Acceleration_array"> <item>meter/sq sec</item> <item>km/sq sec</item> <item>mile/sq sec</item> <item>yard/sq sec</item> </string-array> <string-array name="Angle_array"> <item>degree</item> <item>radian</item> <item>grad</item> <item>gon</item> </string-array>
2 Answers
Accepted Answer
var parentNodeLength = doc.documentElement.getElementsByTagName('string-array').length; for (var i = 0; i < parentNodeLength; i++) { var attrValue = doc.documentElement.getElementsByTagName('string-array').item(i).attributes.getNamedItem('name').nodeValue;
if (attrValue === 'MainCategories') {
var parentNode = doc.documentElement.getElementsByTagName('string-array').item(i);
var subNodeLength = parentNode.getElementsByTagName('item').length;
for (var j = 0; j < subNodeLength; j++) {
var title = parentNode.getElementsByTagName('item').item(j).text;
var row = Ti.UI.createTableViewRow({
height : 110
});
var label = Ti.UI.createLabel({
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
text : title
});
row.add(label);
result.push(row);
}
}
}
var win = Ti.UI.createWindow({ backgroundColor : 'white' });
var result = [];
var fileName = 'test.xml'; //save xml file var tableView = Ti.UI.createTableView({ //data : result, width : '100%', height : '100%' });
//This function read the xml based on tag name passed function readXML(fileName) { var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName); var xmltext = file.read().text; var doc = Ti.XML.parseString(xmltext); //Get the length var length = doc.documentElement.getElementsByTagName('item').length;
for (var i = 0; i < length; i++) {
var title = doc.documentElement.getElementsByTagName('item').item(i).text;
var row = Ti.UI.createTableViewRow({
height:110
});
var label= Ti.UI.createLabel({
height: Ti.UI.SIZE,
width: Ti.UI.SIZE,
text: title
});
row.add(label);
result.push(row);
}
}
readXML(fileName); tableView.setData(result); win.add(tableView); win.open();
Your Answer
Think you can help? Login to answer this question!