Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    54
    Thanks
    3
    Thanked 0 Times in 0 Posts

    javascript and XML

    Hi all..

    I am having problems with coding object oriented javascript along with xml in IE..

    My xml is like this:
    <BOOK>
    <AUTHOR>
    ......
    </AUTHOR>
    <AUTHOR>
    ......
    </AUTHOR>
    </BOOK>
    <BOOK>
    .........
    </BOOK> and so on...

    I do the following:
    bookObject = data.getElementsByTagName("BOOK");
    /* data is the XML from server through responseXML */
    bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");


    the second javascript line causes no problems in firefox but IE says
    "Object doesn't support property or method."
    Someone please tell me what am i doing wrong.. This thing is driving me crazy..


    Thanks in advance!
    raghu207

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    the getElementsByTagName() returns a collection, so that if you want to create a new property for one of the objects in that collection, you need an index.

    That means
    Code:
    bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");
    is not a correct statement, You must have something like
    Code:
    bookObject[i].authorList = bookObject[i].getElementsByTagName("AUTHOR");
    in a loop
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    54
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry, made a mistake..

    the first line is :
    bookObject.authorList = bookObject.getElementsByTagName("AUTHOR")[0];

    I used IE script debugger to see if bookObject.getElementsByTagName("AUTHOR")[0] exists and it does.. so somehow the authorList property is not getting created or something like that.. Just to re-iterate, it works fine in firefox and not in IE 6.. Please help, thanks..

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You were not attentive... bookObject is also a collection, as it is created with the same getElementsByTagName() method as well, so that it needs an index...
    Last edited by Kor; 07-17-2007 at 11:37 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    54
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm so sorry! I'm making too many mistakes.

    Here's the code
    bookObject = data.getElementsByTagName("BOOK")[0];
    /* data is the XML from server through responseXML */
    bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");

    and Kor, bookObject is not a collection.. its a single element.. isn't it the first BOOK element of the XML? because i have said data.getElementsByTagName("BOOK")[0]

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Ok, now can we see the whole picture? What's wrong now?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    try using this instead of document.getElementsByTagName()
    it returns a true array instead of a collection, saves a lot of repetitive coding...
    Code:
    function tags(tid) {
      var ob=document.getElementsByTagName(tid);
      var r = [], mx = ob.length;
         for (var z = 0; z < mx; z++) 
              {r[z] = ob[z];}
     return r;
    }//end function 'tags
    
    
    
    
    //  or from a subtag, wrap your return with an array maker:
    function obVals(ob) {var r = [], mx = ob.length;for (var z = 0; z < mx; z++) {r[z] = ob[z];}return r;}
    
    var rez=someDiv.getElementsByTagName(tid)  //BECOMES:
    
    var rez=obVals(someDiv.getElementsByTagName(tid))
    and the result once again is now a true array.
    Last edited by rnd me; 07-19-2007 at 03:48 AM. Reason: clarity

  • #8
    New Coder
    Join Date
    Aug 2006
    Posts
    54
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi all!

    I got the solution a while ago, but forgot to update you guys..

    The problem was IE is more strict with its object creation.. Firefox seems to be more lenient somehow.. The object(s) that were storing the XML data were completely out of gear. Learned my lesson though..

    Appreciate the help!
    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •