PDA

View Full Version : Resolved how to read data from xml file to javascript array?



onoprise
12-04-2008, 12:09 PM
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:

<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:

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.

onoprise
12-06-2008, 12:09 AM
made some progress. i am now able to read data from the xml file using:


xmlDoc=loadXMLDoc("gamedata.xml");

i can also assign part of the data to another array using:


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:


alert(arrayClues);
shows "3-","0","4*","3 ","7+","3 ","0","0","0","2/","0","3-","2/","0","3","0","E"


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.

clueful
12-06-2008, 03:11 AM
when display the array through an alert, it either displays the entire array or each character as a value:


alert(arrayClues);
shows "3-","0","4*","3 ","7+","3 ","0","0","0","2/","0","3-","2/","0","3","0","E"


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(',');

onoprise
12-06-2008, 09:37 PM
arrayClues=xmlDoc.getElementsByTagName('clue')[0].firstChild.nodeValue.replace(/\"/g,'').split(',');


that worked perfectly! thanks!