任何人都明白为什么这不起作用?!
我已经好几天了,我承认自己是一个新手。
我在浏览器中使用捕获的一组数据尝试使用javascript,这很好。
将它转换为在Appcelerator Titanium中工作,当它到达getAttribute时它就会崩溃,无论我如何尝试并格式化它。
有人说这是数据进入,但似乎没问题,必须是我,它为成千上万的人工作。
任何人帮助我摆脱这种困境并告诉我我做错了什么?
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
var xmlDoc = this.responseXML.documentElement;
var xlinestatus=xmlDoc.getElementsByTagName('LineStatus');
var xline=xmlDoc.getElementsByTagName('Line');
var xstatus=xmlDoc.getElementsByTagName('Status');
var newname = '';
for (i=0;i<xlinestatus.length;i++)
{
Ti.API.info(i);
newname = xlinestatus.getElementsByTagName("Line ID")[0].getAttribute("Name");
// newname = xlinestatus[i].getAttribute('Name');
Ti.API.info(newname);
// Ti.API.info(': ' + '</b>');
// Ti.API.info(xstatus[i].getAttribute("Description"));
// Ti.API.info("<br />");
// Ti.API.info(xlinestatus[i].getAttribute("StatusDetails"));
// Ti.API.info("<br />");
// Ti.API.info("<br />");
}
};
// open the client
xhr.open('GET','http://cloud.tfl.gov.uk/TrackerNet/LineStatus');
// xhr.open('GET','demodata.xml');
// send the data
xhr.send();
答案 0 :(得分:1)
在这一行:
newname = xlinestatus.getElementsByTagName("Line ID")[0].getAttribute("Name");
XML没有名为“Line ID”的元素。它是一个名为“Line”的元素,它有一个名为“ID”的属性:
<Line ID="1" Name="Bakerloo"/>
答案 1 :(得分:1)
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
var xmlDoc = Ti.XML.parseString(this.responseText).documentElement;
var xlinestatus = xmlDoc.getElementsByTagName('LineStatus');
for (i = 0; i < xlinestatus.length; i++) {
var theItem = xlinestatus.item(i);
var newname = theItem.getElementsByTagName("Line").item(0).getAttribute("Name");
var desc = theItem.getElementsByTagName("Status").item(0).getAttribute("Description");
var active = theItem.getElementsByTagName("Status").item(0).getAttribute("IsActive");
Ti.API.info(" Line: " + newname + " Status: " + desc + ", Active: " + active);
}
};
// open the client
xhr.open('GET', 'http://cloud.tfl.gov.uk/TrackerNet/LineStatus');
// send the data
xhr.send({});
提供此输出
[INFO] Line: Bakerloo Status: Good Service, Active: true
[INFO] Line: Central Status: Good Service, Active: true
[INFO] Line: Circle Status: Minor Delays, Active: true
[INFO] Line: District Status: Severe Delays, Active: true
[INFO] Line: Hammersmith and City Status: Minor Delays, Active: true
[INFO] Line: Jubilee Status: Good Service, Active: true
[INFO] Line: Metropolitan Status: Part Suspended, Active: true
[INFO] Line: Northern Status: Good Service, Active: true
[INFO] Line: Piccadilly Status: Minor Delays, Active: true
[INFO] Line: Victoria Status: Good Service, Active: true
[INFO] Line: Waterloo and City Status: Good Service, Active: true