Search code examples
androidxmlmobileappcelerator

Error using getAttribute, why?


I am using Appcelerator Titanium for an Android app and the code below generates an error in the Android emulator, not sure if its my Javascript OR something in Appcelerator.

It objects to the getAttribute for some reason and give the following error:

TypeError: Cannot call method "getAttribute" of undefined (app://pages/xml.js#12)

(it may not be line #12 below due to formatting!! Thx)

Can anyone shed any light onto why the getAttribute is causing a problem?

Here is the code:

// BEGIN SAMPLE CODE

var xhr = Titanium.Network.createHTTPClient();

xhr.onload = function() 
   { 
      var xmlDoc = this.responseXML.documentElement;
      var xlocalestatus=xmlDoc.getElementsByTagName('Locations');
      var xbooks=xmlDoc.getElementsByTagName('Books');
      var newname = '';

      for (i=0;i<xlocalestatus.length;i++) 
         { 
          newname = xbooks[i].getAttribute('Name');
          Ti.API.info(newname);
         }
   };

// open the client

xhr.open('GET','http://myurl.com');

// send the data

xhr.send();


// END SAMPLE CODE

Code also available on Pastie: here http://pastie.org/1670908

Thanks


Solution

  • It's saying you're calling getAttribute on an undefined value, that is, xbooks[i] is undefined.

    This probably indicates that you're not getting the expected result returned from the server you're contacting. You should examine exactly what's coming back, and exactly what xbooks and/or its elements are being set to.