Javascript 为什么我的XML代码在getAttribute中失败?
有人能解释为什么这行不通吗 我已经做了好几天了,我承认自己是个新手 我在一个浏览器中用javascript和一组捕获的数据进行了尝试,结果很好 将它转换为在Appcelerator Titanium中工作,当它到达getAttribute时,不管我如何尝试格式化它,它都会失效 有人说是数据进来了,但这似乎没问题,必须是我,它为成千上万的其他人工作 有谁能帮我摆脱这种习惯,告诉我我做错了什么Javascript 为什么我的XML代码在getAttribute中失败?,javascript,xml,titanium,appcelerator,Javascript,Xml,Titanium,Appcelerator,有人能解释为什么这行不通吗 我已经做了好几天了,我承认自己是个新手 我在一个浏览器中用javascript和一组捕获的数据进行了尝试,结果很好 将它转换为在Appcelerator Titanium中工作,当它到达getAttribute时,不管我如何尝试格式化它,它都会失效 有人说是数据进来了,但这似乎没问题,必须是我,它为成千上万的其他人工作 有谁能帮我摆脱这种习惯,告诉我我做错了什么 var xhr = Titanium.Network.createHTTPClient(); xhr.o
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();
var xhr=Titanium.Network.createHTTPClient();
xhr.onload=函数()
{
var xmlDoc=this.responseXML.documentElement;
var xlinestatus=xmlDoc.getElementsByTagName('LineStatus');
var xline=xmlDoc.getElementsByTagName('Line');
var xstatus=xmlDoc.getElementsByTagName('Status');
var newname='';
对于这一行中的(i=0;i)
newname = xlinestatus.getElementsByTagName("Line ID")[0].getAttribute("Name");
XML没有名为“Line ID”的元素。它是名为“Line”的元素,具有名为“ID”的属性:
XML是否完全有效?您是否使用XML验证器对其进行了验证?您似乎不太可能有一个名为“Line ID”的标记来调用getAttribute()…如果我更改键入方式,我会得到以下信息:message=“表达式'xlinestatus.getElementsByTagName(\'Line\')”[null]不是对象。”;name=TypeError;我不熟悉Tianium框架,但在我看来xlinestatus可能是一个数组,因此需要执行类似xlinestatus[I]的操作。getElementsByTagName(“Line”)…这个答案是错误的,因为在Tianium框架中,getElementsByTagName不返回数组。请参阅Aaron使用的答案。item()。非常感谢Aaron,我将涉过这一过程,了解我的错误所在,并从中吸取教训。谢谢。我仍然无法从LineStatus中获取状态详细信息。显然,我在遍历树时遇到了问题。我如何获取该信息?目前该信息可能为空,但通常其中包含一些数据。当是否可用?我尝试了以下方法:
<Line ID="1" Name="Bakerloo"/>
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