Results 1 to 4 of 4

Thread: how to read data from xml file to javascript array?

  1. #1
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to read data from xml file to javascript array?

    i need help reading data from an xml file to a javascript array. from searching online, all i could find was how to store everything into one array, but i want to do something a little different.

    below is an example xml file with 1 item consisting of 4 element:
    Code:
    <games>
    <game>
    <answer>"1","4","2","3","4","3","1","2","3","2","4","1","2","1","3","4"</answer>
    <clue>"3-","0","4*","3 ","7+","3 ","0","0","0","2/","0","3-","2/","0","3 ","0"</clue>
    <vertical>"0","0","1","1","0","1","1","0","0","1","0","1","0","0","1","1"</vertical>
    <horizontal>"1","1","0","1","0","1","1","1","1","1","1","0","0","0","0","0"</horizontal>
    </game>
    </games>
    and here are the arrays i'd like the info stored in:
    Code:
    var arrayClues = new Array();
    var arrayVertical = new Array();
    var arrayHorizontal = new Array();
    var arrayAnswers = new Array();
    please let me know if you can help. thanks in advance.
    Last edited by onoprise; 12-07-2008 at 12:10 AM.

  2. #2
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    made some progress. i am now able to read data from the xml file using:

    Code:
    xmlDoc=loadXMLDoc("gamedata.xml");
    i can also assign part of the data to another array using:

    Code:
    var arrayClues = new Array();
    var arrayClues = xmlDoc.getElementsByTagName("clue")[0].childNodes[0].nodeValue;
    but when display the array through an alert, it either displays the entire array or each character as a value:

    Code:
    alert(arrayClues);
    shows "3-","0","4*","3 ","7+","3 ","0","0","0","2/","0","3-","2/","0","3","0","E"
    Code:
    alert(arrayClues[0]);
    shows "
    what can i do to properly display the data? (3- 0 4* etc)

    do i need to change the javascript code or the xml file or both?

    if anyone has any advise, lemme know. thanks in advance.

  3. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by onoprise View Post
    when display the array through an alert, it either displays the entire array or each character as a value:

    Code:
    alert(arrayClues);
    shows "3-","0","4*","3 ","7+","3 ","0","0","0","2/","0","3-","2/","0","3","0","E"
    Code:
    alert(arrayClues[0]);
    shows "
    what can i do to properly display the data? (3- 0 4* etc)
    arrayClues=xmlDoc.getElementsByTagName('clue')[0].firstChild.nodeValue.replace(/\"/g,'').split(',');

  4. The Following User Says Thank You to clueful For This Useful Post:

    onoprise (12-06-2008)

  5. #4
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by clueful View Post
    arrayClues=xmlDoc.getElementsByTagName('clue')[0].firstChild.nodeValue.replace(/\"/g,'').split(',');
    that worked perfectly! thanks!

Bookmarks

Posting Permissions

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