Al1nuX
Al1nuX

Reputation: 478

Getting data from a remote XML file in Appcelerator titanium

I'm trying to parse a very simple XML file.

I need to get one tag element that contains a URL. The tag in the XML file is sURL.

I have tried using the code below but its not returning the text value of the URL. I don't know what i'm doing wrong and why it's not working.

var url = "http://example.com/data.xml"; //xml resource url     
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {   
  var doc = this.responseXML.documentElement;
  var myURL = doc.getElementsByTagName("sURL");
  Ti.API.info(myURL);
};
xhr.onerror = function(e) { 
 alert('Network error '+e.error);
};
xhr.open('GET',url);
xhr.send();

XML :

<rss version="2.0">
<channel>
<title>test</title>
<description>

</description>
<time>test</time>
<time2>test</time2>
<sURL>https://example.com</sURL>
<item>
<title>test</title>
<broadcast>test</broadcast>
<time>17:00</time>
<time2>14:00</time2>
</item>
<item>
<title>test</title>
<broadcast>test</broadcast>
<time>19:00</time>
<time2>16:00</time2>
</item>
<item>
<title>test</title>
<broadcast>test</broadcast>
<time>21:00</time>
<time2>18:00</time2>
</item>
<item>
<title>test</title>
<broadcast>test</broadcast>
<time>23:00</time>
<time2>20:00</time2>
</item>
<item>
<title>test</title>
<broadcast/>
<time>01:00</time>
<time2>22:00</time2>
</item>
</channel>
</rss>

Upvotes: 1

Views: 145

Answers (1)

Kassem Itani
Kassem Itani

Reputation: 1147

check the below code to get the first item of sURL, if it didn't work share the xml file

var url = "https://gist.githubusercontent.com/kassemitani/e25ab8654a4914a7e19edaecd7cb5460/raw/3aad56a246edc8e4dd42b1255c602f8676434a8f/test.xml"; //xml resource url     
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {   
  var doc = this.responseXML.documentElement;
  var myURL = doc.getElementsByTagName("sURL").item(0).textContent;
  Ti.API.info(myURL);
};
xhr.onerror = function(e) { 
 alert('Network error '+e.error);
};
xhr.open('GET',url);
xhr.send();

Upvotes: 1

Related Questions