Skip to content

Instantly share code, notes, and snippets.

@csemrm
Last active August 29, 2015 13:58
Show Gist options
  • Save csemrm/10002696 to your computer and use it in GitHub Desktop.
Save csemrm/10002696 to your computer and use it in GitHub Desktop.
How to access XML element by attribute id?
var self = Ti.UI.createWindow({
backgroundColor : '#fff'
});
var xhr = Titanium.Network.createHTTPClient();
var aTableView = Ti.UI.createTableView();
xhr.onload = function() {
//Ti.API.info('http://appc.mrtechnologybd.com/node.xml ' + this.responseXML + ' text ' + this.responseText);
var doc = this.responseXML.documentElement;
var data = [];
var items = doc.getElementsByTagName("animal");
for (var i = 0; i < items.length; i++) {
var type = items.item(i).getElementsByTagName("type").item(0).getAttribute('id');
data.push({
title : type
});
}
aTableView.setData(data);
self.add(aTableView);
};
xhr.onerror = function(e) {
Ti.API.info('error:' + e.error);
};
// open the client
xhr.open('GET', 'http://appc.mrtechnologybd.com/node.xml');
// send the data
xhr.send();
self.open();
<?xml version="1.0" encoding="utf-8"?>
<animals>
<animal>
<type id="mammal">
<kind>Dog</kind>
<name>Duke</name>
</type>
</animal>
<animal>
<type id="Reptile">
<kind>Frog</kind>
<name>Kermit</name>
</type>
</animal>
</animals>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment