简体   繁体   English

从 Appcelerator Titan 中的远程 XML 文件获取数据

[英]Getting data from a remote XML file in Appcelerator titanium

I'm trying to parse a very simple XML file.我正在尝试解析一个非常简单的 XML 文件。

I need to get one tag element that contains a URL.我需要获取一个包含 URL 的标签元素。 The tag in the XML file is sURL. XML 文件中的标记是 sURL。

I have tried using the code below but its not returning the text value of the URL.我尝试使用下面的代码,但它没有返回 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 : 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>

check the below code to get the first item of sURL, if it didn't work share the xml file检查下面的代码以获取 sURL 的第一项,如果它不起作用共享 xml 文件

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM