gpt4 book ai didi

javascript - 为什么我的 XML 代码在 getAttribute 时失败?

转载 作者:行者123 更新时间:2023-11-30 18:46:25 24 4
gpt4 key购买 nike

有人阐明为什么这不起作用吗?!

我已经用了好几天了,我承认自己是个新手。

我在浏览器中使用捕获的数据集尝试将此作为 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();

最佳答案

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

关于javascript - 为什么我的 XML 代码在 getAttribute 时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5313752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com