开发者

Why does my XML code fail at getAttribute?

Anyone shed any light onto why this doesn't work?!

I've been at it for days now, I admit to being a bit of a newbie.

I tried this as javascript in a browser with a captured set of the data and it was fine.

Converted it to work in Appcelerator Titanium and it just craps out when it gets to the getAttribute, no 开发者_如何学编程matter how I try and format it.

Someone said it was the data coming in but that seems to be ok, has to be me, its working for thousands of other people.

Anyone help get me out of this rut and tell me what I am doing wrong?

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

provided this output

[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


In this line:

newname = xlinestatus.getElementsByTagName("Line ID")[0].getAttribute("Name");

The XML doesn't have an element called "Line ID". It's an element called "Line" that has an attribute called "ID":

<Line ID="1" Name="Bakerloo"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜